没有从gridview中的CommandArgument获取正确的值 [英] not getting correct value from CommandArgument in gridview
问题描述
Hello all
i我试图使用CommandArgument从我的GridView获取ID值,但是当它返回行索引值而不是ID值时
< asp:TemplateField HeaderText = HeaderStyle-HorizontalAlign = 中心 >
< ItemTemplate>
< asp:ImageButton ID = imgbtnDelete CommandName = Delete1 CommandArgument = ' <%#Eval(VisID)%>' runat = server ImageUrl = images / button_delete.png />
< / ItemTemplate >
< HeaderStyle HorizontalAlign = 中心 />
< / asp:TemplateField >
这是我的C#代码:
protected void grdAppuser_RowDataBound( object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton delBtn =(ImageButton)e.Row.FindControl( imgbtnDelete跨度>);
delBtn.Attributes.Add( OnClick, 返回确认('您确定要删除'););
e.Row.Attributes [ onclick] = ClientScript.GetPostBackClientHyperlink ( this .grdAppuser, 选择$ + e.Row.RowIndex);
}
受保护 void grdAppuser_RowCommand(< span class =code-keyword> object sender,GridViewCommandEventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo( en-US);
尝试
{
switch (e。 CommandName)
{
case ( Delete1):
string id = Convert.ToString(e.CommandArgument); // 这里的问题此行返回行索引而不是ID
}
所以我说e.CommandArgument返回行索引而不是ID值。
请帮助
谢谢
+ e.Row.RowIndex) ;
}
受保护 void grdAppuser_RowCommand( object sender,GridViewCommandEventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo( en-US);
尝试
{
开关(e.CommandName)
{
case ( Delete1):
string id = Convert.ToString(e.CommandArgument); // 这里的问题此行返回行索引而不是ID
}
所以我说e.CommandArgument返回行索引而不是ID值。
请帮助
谢谢
首先,从代码中删除以下行
e.Row.Attributes [ onclick] = ClientScript.GetPostBackClientHyperlink( this .grdAppuser, 选择
+ e.Row.RowIndex);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo( en-US);
然后添加以下你的gridView aspx事件
< asp:gridview id = grdAppuser runat = server autogeneratecolumns = false xmlns:asp = #unknown >
OnRowCommand =grdAppuser_RowCommand OnRowDataBound =grdAppuser_RowDataBoundWidth =90%>
< asp:imagebutton id = imgbtnDelete commandname = Delete1 commandargument = <% #Eval( VisID)%> runat = server imageurl = images / button_delete.png / >
< / asp:gridview >
然后添加在你的行命令
事件中,你肯定会得到e.CommandArgument
值
< br $> b $ b
受保护 void grdAppuser_RowCommand( object sender,GridViewCommandEventArgs e)
{
switch ( e.CommandName)
{
case ( Delete1):
string id = Convert.ToString(e.CommandArgument);
}
}
希望这对您有用。
Hello all
i am trying to get ID value from my GridView using CommandArgument, but when it is return the row index value instead of ID value
<asp:TemplateField HeaderText=" " HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete1" CommandArgument='<%# Eval("VisID") %>' runat="server" ImageUrl="images/button_delete.png"/>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
and this is my C# code:
protected void grdAppuser_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton delBtn = (ImageButton)e.Row.FindControl("imgbtnDelete");
delBtn.Attributes.Add("OnClick", "return confirm('Are you sure you want to delete');");
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdAppuser, "Select$" + e.Row.RowIndex);
}
protected void grdAppuser_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
try
{
switch (e.CommandName)
{
case ("Delete1"):
string id = Convert.ToString(e.CommandArgument); // the problem here this line return row index instead of ID
}
so as i said the e.CommandArgument return row index instead of ID value.
please help
thank you
" + e.Row.RowIndex); } protected void grdAppuser_RowCommand(object sender, GridViewCommandEventArgs e) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); try { switch (e.CommandName) { case ("Delete1"): string id = Convert.ToString(e.CommandArgument); // the problem here this line return row index instead of ID }
so as i said the e.CommandArgument return row index instead of ID value.
please help
thank you
First of all, remove following lines from your code
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdAppuser, "Select
" + e.Row.RowIndex); System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Then Add following events to your gridView aspx
<asp:gridview id="grdAppuser" runat="server" autogeneratecolumns="false" xmlns:asp="#unknown"> OnRowCommand="grdAppuser_RowCommand" OnRowDataBound="grdAppuser_RowDataBound" Width="90%"> <asp:imagebutton id="imgbtnDelete" commandname="Delete1" commandargument="<%#Eval("VisID")%>" runat="server" imageurl="images/button_delete.png" /> </asp:gridview>
Then add this in yourRow Command
event, you will surely gete.CommandArgument
value
protected void grdAppuser_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case ("Delete1"): string id = Convert.ToString(e.CommandArgument); } }
Hope this will work for you.
这篇关于没有从gridview中的CommandArgument获取正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!