如何将页面中的Button类型转换为GridView中的按钮类型 [英] how to cast Button type in page to button type in GridView
本文介绍了如何将页面中的Button类型转换为GridView中的按钮类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无法投射类型' object code-string> System.Web.UI.WebControls.Button'键入' System.Web .UI.WebControls.GridView'。
此按钮在页面中
protected void BtnSave_OnClick( object sender,EventArgs e)
{
btnRewrk_OnClick(sender,e);
}
这里我想调用波纹管事件方法,其中按钮在gridview中,所以我在这里得到错误:
无法转换类型' object -string> System.Web.UI.WebControls.Button'键入' System.Web.UI .WebControls.GridView'。
此按钮位于gridview中
protected void btnRewrk_OnClick( object sender,EventArgs e)
{
GridViewRow row =(GridViewRow)(((Control)sender).NamingContainer);
}
解决方案
HI anbujeremiah,
这是一个可以帮助您找到解决方案的链接,请查看它。
代码背后的访问按钮
首先将按钮添加到网格中,如下面的代码片段所示:
< asp: TemplateField >
< ItemTemplate >
< asp:LinkButton ID 跨度> <跨度class =code-keyword> = lnkSil runat = server CommandName = bla
CommandArgument =' <%#((GridViewRow) )容器).RowIndex %> ' 文字 = Sil > < / asp:LinkButton >
< < span class =code-leadattribute> / ItemTemplate >
< / asp:TemplateField >
然后添加一个能够访问控件的行数据绑定函数。请按照以下代码段进行操作。
受保护 void GridView1_RowCommand( object sender,GridViewCommandEventArgs e)
{
int currentRowIndex = Int32 .Parse(e.CommandArgument.ToString());
LinkButton bf =(LinkButton)gv.Rows [currentRowIndex] .Cells [ 1 ]。控件[ 0 < /跨度>];
...
}
谢谢&问候
sisir
而不是这个使用这个
< asp:button runat = server id = BtnSave rowindex = < %#Container.DisplayIndex%> >
OnClick = BtnSave_OnClick />
< / asp:按钮 >
受保护 void BtnSave_OnClick( object sender,EventArgs e){
Button ibtn1 = sender as 按钮;
int rowIndex = Convert.ToInt32(ibtn1.Attributes [ rowIndex位置跨度>]);
// 在代码中使用此rowIndex来获取单击的按钮
GridViewRow row = Gridview.rows [rowIndex];
}
你不需要在这里调用另一个按钮的事件。相反,你可以使用常用方法进行这两个事件。 / BLOCKQUOTE>
Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.GridView'.
this button is in page
protected void BtnSave_OnClick(object sender, EventArgs e) { btnRewrk_OnClick(sender,e); }
here i want to call the bellow event method in which button is in gridview, so here i am getting error:
Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.GridView'.
This button is in gridview
protected void btnRewrk_OnClick(object sender, EventArgs e) { GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer); }解决方案HI anbujeremiah,
This is a link that can help you in finding your solution please have a look into it.
Access buttons in Code behind
First of all add the button to your grid as following code snippet:
<asp:TemplateField> <ItemTemplate> <asp:LinkButton ID = "lnkSil" runat="server" CommandName="bla" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' Text= "Sil" ></asp:LinkButton> </ItemTemplate> </asp:TemplateField>
Then add a row data bound function that can able to access the control. Follow the below code snippet.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int currentRowIndex = Int32.Parse(e.CommandArgument.ToString()); LinkButton bf = (LinkButton)gv.Rows[currentRowIndex].Cells[1].Controls[0]; ... }
Thanks & Regards
sisir
Instead of this use this
<asp:button runat="server" id="BtnSave" rowindex="<%# Container.DisplayIndex %>" > OnClick="BtnSave_OnClick"/> </asp:button>
protected void BtnSave_OnClick(object sender, EventArgs e){ Button ibtn1 = sender as Button; int rowIndex = Convert.ToInt32(ibtn1.Attributes["RowIndex"]); //Use this rowIndex in your code to get which button is clicked GridViewRow row = Gridview.rows[rowIndex]; }
You dont nedd to call event of another button here.Instead you can use common method for both event.
这篇关于如何将页面中的Button类型转换为GridView中的按钮类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文