Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

c# - How do I programmatically add a button to a gridview and assign it to a specific code-behind function?

In runtime I'm creating a DataTable and using nested for-loops to populate the table. This table I later assign as DataSource to a gridview and on RowDataBound I assign the value of each cell. I want to know how I can give each cell a button and assign that button to a codebehind function. I'll have 12 buttons and each one will contain a different value. I would prefer if they all call the same function with some kind of event that stores the cell-specific value.

This is the code where the Table gets created:

protected void GridViewDice_RowDataBound(object sender, GridViewRowEventArgs e)
{


    DataTable diceTable = _gm.GetDice(_gameId);
    for (int i = 0; i < GameRules.ColumnsOfDice; i++)
    {
        if(e.Row.RowIndex > -1)
        {
            /*This is where I'd like to add the button*/
            //e.Row.Cells[i].Controls.Add(new Button);
            //e.Row.Cells[i].Controls[0].Text = specific value from below

            //This is where the specific value gets input
            e.Row.Cells[i].Text = diceTable.Rows[e.Row.RowIndex][i].ToString();
        }

    }
}

I would like to handle the buttonclick with something like this:

protected void DiceButton_Click(int column, int row, int value)
{
    //Do whatever
}

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On your gridview in markup, assign CommandArgument attribute to whichever you want (here I choose the index of the current gridviewrow) inside the your buttons.

 <asp:Button ID="lbnView" runat="server" Text="Button" OnClick="btn_Clicked" 
CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"></asp:Button>

Or in your code behind, you can create a button like below

protected void GridViewDice_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 


    DataTable diceTable = _gm.GetDice(_gameId); 
    for (int i = 0; i < GameRules.ColumnsOfDice; i++) 
    { 
        if(e.Row.RowIndex > -1) 
        { 
            Button btn = new Button();
            btn.CommandArgument = diceTable.Rows[e.Row.RowIndex][i].ToString(); 
            btn.Attributes.Add("OnClick", "btn_Clicked");

            e.Row.Cells[i].Controls.Add(btn);
        }
    }
}

then make an event handler like below

protected void btn_Clicked(object sender, EventAgrs e)
{
   //get your command argument from the button here
   if (sender is Button)
   {
     try
     {
        String yourAssignedValue = ((Button)sender).CommandArgument;
     }
     catch
     {
       //Check for exception
     }
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...