简体   繁体   中英

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. Is it possible? for example [] linkbtn1 [] linkbtn2 when i check on first checkbox the id not text of linkbutton1 should be fetched.([] is 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. You can retrieve that SNA value by using the check box's CheckChanged event, like this:

In the DataList 's ItemTemplate markup:

<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:

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;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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