开发者

GridView from CodeBehind select row and postback

开发者 https://www.devze.com 2023-02-15 14:37 出处:网络
I am having to create a GridView 100% in C# CodeBehind.I have it selecting a row and posting back using this code:

I am having to create a GridView 100% in C# CodeBehind. I have it selecting a row and posting back using this code:

    void dataGrid_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            e.Item.Attributes.Add("onmouseover",
                   "this.style.backgroundColor='beige';this.style.cursor='pointer'");
            e.Item开发者_如何学JAVA.Attributes.Add("onmouseout",
                   "this.style.backgroundColor='#FFFFFF';");
            e.Item.Attributes.Add("onclick", "javascript:__doPostBack" +
                   "('_ctl0$DataGrid1$_ctl" +
                   ((Convert.ToInt32(e.Item.ItemIndex.ToString())) + 2) +
                   "$_ctl0','')");
        }
    }

This does post back but then how do I get the ID of the row the user clicked on?


void dataGrid_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.Item)
        {
            var item = e.Item.DataItem;  // <- entity object that's bound, like person
            var itemIndex = e.Item.ItemIndex; // <- index
        }

    }


You can pass an argument in the second paramater to __doPostBack:

__doPostBack(controlname, yourid);

So fill it in here:

e.Item.Attributes.Add("onclick", "javascript:__doPostBack" +
    "('_ctl0$DataGrid1$_ctl" +
   ((Convert.ToInt32(e.Item.ItemIndex.ToString())) + 2) + 
   "$_ctl0','PUT YOUR VALUE HERE')");

Then you can access it through the event arguments.

0

精彩评论

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