如何访问传递给锚标记的参数以使用 Javascript 单击链接按钮

这是我用来在客户端显示所有标签的代码......我想将锚标签功能上的参数传递到服务器端链接按钮单击


<ul class="list_load">

    <% for (int i = 0; i < dtscrippsyearcount.Rows.Count; i++)

        { %>

    <li class="list_item">

        <asp:LinkButton runat="server" ID="lnkdisplaytag" OnClick="lnkdisplaytag_Click"></asp:LinkButton>

        <a href="#" id="<%=dtscrippsyearcount.Rows[i]["TagId"].ToString() %>" onclick="javascript:GetSelectedTag('<%=dtscrippsyearcount.Rows[i]["TagId"].ToString() %>')"><%=dtscrippsyearcount.Rows[i]["TagName"].ToString() %></a></li>

    <%} %>

</ul>            



function GetSelectedTag(Tagid) {

    __doPostBack('lnkdisplaytag ', Tagid)

    $("#lnkdisplaytag").click();

     return true;

}

背后的代码


protected void lnkdisplaytag_Click(object sender, EventArgs e)

{

    string parameter = Request["__EVENTARGUMENT"];////want to get tagid here

}


慕田峪9158850
浏览 123回答 1
1回答

Smart猫小萌

我建议您开始使用实际的控件,例如中继器,而不是创建内联循环。在下面的代码片段中,一个简单的工作示例说明了如何使用中继器,在单击 LinkButton 时用数据填充它并获取 TagID。首先,在 ItemTemplate 中使用 LinkButton 向 aspx 页面添加一个 Repeater。注意OnCommand替代OnClick和CommandArgument属性的使用。<ul class="list_load">&nbsp; &nbsp; <asp:Repeater ID="Repeater1" runat="server">&nbsp; &nbsp; &nbsp; &nbsp; <ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="list_item">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:LinkButton runat="server" ID="lnkdisplaytag"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OnCommand="lnkdisplaytag_Command" CommandArgument='<%# Eval("TagId") %>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text='<%# Eval("TagName") %>'></asp:LinkButton>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ItemTemplate>&nbsp; &nbsp; </asp:Repeater></ul>要填充中继器,您需要在后面的代码中执行以下操作。protected void Page_Load(object sender, EventArgs e){&nbsp; &nbsp; if (IsPostBack == false)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Repeater1.DataSource = dtscrippsyearcount;&nbsp; &nbsp; &nbsp; &nbsp; Repeater1.DataBind();&nbsp; &nbsp; }}然后,当单击 LinkButton 时,您可以CommandArgument轻松获取该值。protected void lnkdisplaytag_Command(object sender, CommandEventArgs e){&nbsp; &nbsp; Label1.Text = e.CommandArgument.ToString();}
打开App,查看更多内容
随时随地看视频慕课网APP