开发者

how to grab the Ctl from a linkbutton

I have a gridview which is pulling it data from a stored procedure. In the column title, I have used a Linkbutton. I would like to grab the Ctl ID and store it in a variable when it is clicked. I am not sure what is happening, but I get a few strings going through. Below is my code and this is similar to my previous post.

ASP开发者_如何转开发.NET

<asp:TemplateField HeaderText="ID">
            <ItemTemplate>
            <asp:LinkButton ID="lnkID" runat="server" OnClick="lblClick1"
            Text='<%#Eval("ID") %>'>
            </asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>

C#:

 GridViewRow row = gv2.SelectedRow;
    string controlId = ((LinkButton)row.FindControl("lnkID")).ID;
    lblshow.Text = controlId;

Can someone help me get up and running. Thanks.


You could use a hidden field to store the id:

<ItemTemplate>
    <asp:HiddenField 
        runat="server" 
        ID="ID" 
        Value='<%# Eval("ID") %>' 
    />

    <asp:LinkButton 
        runat="server" 
        OnClick="LabelClick" 
        Text="click me" 
    />
</ItemTemplate>

and then:

protected void LabelClick(object sender, EventArgs e)
{
    var hiddenField = (HiddenField)((Control)sender).FindControl("ID");
    var id = hiddenField.Value;
    // Do something with the id        
}

UPDATE:

Full working example:

<%@ Page Language="C#" %>
<script type="text/c#" runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grid.DataSource = Enumerable.Range(1, 10).Select(x => new
            {
                ID = x
            });
            grid.DataBind();
        }
    }

    protected void LabelClick(object sender, EventArgs e)
    {
        var hiddenField = (HiddenField)((Control)sender).FindControl("ID");
        result.Text = string.Format("selected id: {0}", hiddenField.Value);
    }
</script>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:HiddenField 
                        runat="server" 
                        ID="ID" 
                        Value='<%#Eval("ID") %>' 
                    />
                    <asp:LinkButton 
                        runat="server" 
                        OnClick="LabelClick" 
                        Text="click me" 
                    />
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

        <asp:Label ID="result" runat="server" />
    </form>
</body>
</html>


In the OnClick method lblClick1 you can use:

public void lblClick1(object sender, CommandEventArgs e)
{
    int controlId = Convert.ToInt32(e.CommandArgument); //get ID and store it in controlId
    lblshow.Text = controlId;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜