开发者

writing code in button's click event in gridview

i have added a button control in gridview and i want to redirect to the another page on button's click event, and also i want to access the values of selected row in a gridview and show that values on other page on button click event which is inside the开发者_JAVA技巧 gridview,, can anybody plz help me out in this .


this should be helpful: http://msdn.microsoft.com/en-us/library/bb907626.aspx

for accessing values inside RowCommand event find your row first (by index):

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = MyGridView.Rows[index];

and then controls inside it which holds the values you need:

Label MyLabel= (Label)row.FindControl(MyLabel);

after that all you need to do is to transfer to your page and send values from your controls with it (do that with querystring):

Server.Transfer("MyPage.aspx?value="+MyLabel.Text)

here you can read more about gridview RowCommand event: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx


You need to add either a ButtonField or a TemplateField with a button inside and then you can add your code to the RowCommand and use the e.CommandName and e.CommandArgument

To redirect you can use Response.Redirect(address)

To see the values it depends where in the program you want to do it. If during databinding you can use the dataitem("column"). Outside of databinding you can go through the Gridview.Rows collection for the row you want and then look at the row.Cells(columnNumber).Text to see the value.


we have an event in gridview called, gridview rowcommand event, in that you can write the code for redirecting to another page using button. First you have to bind the value of which you want to pass in button property called commandargument.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Response.Redirect("abc.aspx?id=" + e.CommandArgument);
    //here id means passing the value using querystring to another page.
}


 <asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="ID" />
        <asp:BoundField HeaderText="Name" DataField="fullname" />
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:Button runat="server" ID="btnDelete" Text="Delete" OnClick="btnDelete_Click" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

// This focuses the current row where the button lies. you can perform all events of server-controlls //instead of Button e.g LinkButton, Dropdownlist index changes...

 protected void btnDelete_Click(object sender, EventArgs e)
    {


        Button btn = (Button)sender;
        GridViewRow gvRow = (GridViewRow)btn.NamingContainer;

        string id = gvRow.Cells[0].Text;
        string name = gvRow.Cells[1].Text;
        //use these values in query string or any logic you prefer for cross page posting
        Response.Redirect("your URL ?id=" + id + "&name=" + name);

    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜