简体   繁体   English

如何使用c#单击数据列表中的复选框来查找linkbutton值?

[英]how to find linkbutton value on clicking checkbox in datalist using c#?

this is what i have done: 这是我所做的:

<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">
                                <ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="TabPanel1">
                                    <HeaderTemplate>
                                        Pro
                                    </HeaderTemplate>
                                    <ContentTemplate>

                                        <div style="height: 100px; width: 200px; overflow: Auto">
                                            <asp:DataList ID="DataList1" runat="server" CellPadding="2" CellSpacing="2" ForeColor="Red">
                                                <ItemTemplate>
                                                    <asp:CheckBox ID="checkbox" runat="server" AutoPostBack="true" OnCheckedChanged="checkLike_CheckedChanged" />
                                                    <asp:LinkButton Text='<%#Eval("UPP")%>' ID="lnkpro" runat="server" CssClass="linkbutton"
                                                        OnClick="btn_Click" CommandArgument='<%# Eval("UCode") %>'></asp:LinkButton>
                                                </ItemTemplate>


                                            </asp:DataList>
                                        </div>

                                    </ContentTemplate>
                                </ajaxToolkit:TabPanel>
                                <ajaxToolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="services">
                                    <HeaderTemplate>
                                        Sets
                                    </HeaderTemplate>
                                    <ContentTemplate>

                                        <div style="height: 100px; width: 200px; overflow: Auto">
                                            <asp:DataList ID="DataList2" runat="server" CellPadding="2" CellSpacing="2">
                                                <ItemTemplate>
                                                    <asp:CheckBox ID="checkbox" runat="server" />
                                                    <asp:LinkButton Text='<%#Eval("SNA")%>' ID="lnkpro1" runat="server" CssClass="linkbutton"
                                                        OnClick="btn_Click1" CommandArgument='<%# Eval("Code") %>'></asp:LinkButton>
                                                </ItemTemplate>
                                            </asp:DataList>
                                        </div>
                                    </ContentTemplate>
                                </ajaxToolkit:TabPanel>
                            </ajaxToolkit:TabContainer>

Now my question : This code block results in a list of link buttons in front of them all checkboxes. 现在我的问题是:此代码块会在所有复选框前面显示一个链接按钮列表。 So what i want is to get the respective linkbutton value on checkbox checkchanged. 所以我想要的是在checkchanged复选框上获取相应的linkbutton值。 Is it possible? 可能吗? for example [] linkbtn1 [] linkbtn2 when i check on first checkbox the id not text of linkbutton1 should be fetched.([] is checkbox ) 例如[[] linkbtn1 [] linkbtn2,当我在第一个复选框上选中时,应提取id而不是linkbutton1的文本。[[]是checkbox)

The ID of each link button is going to be the same on each row of the data list, the text value of each link button will be the SNA value bound to that link button. 每个链接按钮的ID在数据列表的每一行上都将是相同的,每个链接按钮的文本值将是绑定到该链接按钮的SNA值。 You can retrieve that SNA value by using the check box's CheckChanged event, like this: 您可以通过使用复选框的CheckChanged事件来检索该SNA值,如下所示:

In the DataList 's ItemTemplate markup: DataListItemTemplate标记中:

<asp:CheckBox ID="checkbox" runat="server" 
              OnCheckedChanged="checkbox_CheckChanged" />

Now that we have wired up the check changed event, it is time to implement the handler, like this: 既然我们已经连接了check change事件,就该实现处理程序了,如下所示:

protected void checkbox_CheckChanged(object sender, EventArgs e) 
{
    CheckBox theCheckBox = sender as CheckBox;

    // Make sure the cast to check box worked before we try to use it
    if(theCheckBox != null)
    {
        LinkButton theLinkButton = theCheckBox.Parent.FindControl("lnkpro1") as LinkButton;

        // Verify that the link button was found before we try to use it
        if(theLinkButton != null)
        {
            // Get the text value from the link button in the same row 
            // as the check box checked changed
            string theLinkButtonText = theLinkButton.Text;

            // Do something with text value here

        }
    }
}

Try this 尝试这个

protected void checkbox_CheckedChanged(object sender, EventArgs e)
    {
        foreach (DataListItem item in DataList1.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("checkbox");
            LinkButton lnkbtnActivate = (LinkButton)item.FindControl("lnkpro");

            if (chk.Checked == true)
            {
                string Result = lnkbtnActivate.Text;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM