button控件的CommandArgument属性值应该设置为什么?

newty_won 2008-11-14 02:23:55
if (e.CommandName == "Add")
{
foreach (GridViewRow row in this.GridView1.Rows)
{
if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString())
{
TextBox txt = (TextBox)row.FindControl("txtType");
string strConn = ConfigurationManager.ConnectionStrings["DB_website"].ConnectionString;

SqlDataSource ds = new SqlDataSource();
ds.ConnectionString = strConn;
ds.InsertCommand = string.Format("insert into tb_websitetype values({0},'{1}')", 1, txt.Text);
if (ds.Insert() > 0)
{
wmb.MessageBox("添加类型成功~");
DropDownList ddl = (DropDownList)row.FindControl("ddl");
ddl.DataBind();
}
}
}
}

在这段代码中,e.CommandArgument的值会是什么? 那么button属性的CommandArgument值应该设置为什么?
...全文
513 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
newty_won 2008-11-17
  • 打赏
  • 举报
回复
兄弟 你的解释很不错 但是我结贴的时候看错了 分数给低了 谅解~
Jack_xiao 2008-11-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 newty_won 的回复:]
请问下 为什么
调试的时候 TextBox txt = (TextBox)row.FindControl("txtType");
txt的值是空的?
[/Quote]
先确认下界面上有没有生成TextBox
可以在TextBox中写入固定值,看看界面上有没有显示
bch520 2008-11-14
  • 打赏
  • 举报
回复
<asp:LinkButton ID="lkbtn" runat="server" CommandArgument='<%# Eval("id")%>'></asp:LinkButton>
claymore1114 2008-11-14
  • 打赏
  • 举报
回复
1、CommandArgument 是获取或设置与关联的 CommandName 属性一起传递到 Command 事件处理程序的可选参数,是 CommandName的补充,可以不设置。
2、(1)e.CommandArgument是取得按下Button按钮字段所在的行索引,以便判断是哪行被按下Button按钮,
再通过此索引来取得实际GridViewRow对象(数据行)。
(2)e.CommandName是BoundField/..字段中的CommandName属性(Order或CancelOrder)设置值,
这两个参数的搭配应用实际上只是为了识别Button按钮字段坐标,因为GridView中有一堆按钮,
系统根本无从知道用户按下哪个按钮,故通过e.CommandArgument取得Y轴坐标(Row),
而e.CommandName取得X轴坐标(Column),有了X与Y坐标,就可以找到选择的是哪一行。

criedshy 2008-11-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 newty_won 的回复:]
请问下 为什么
调试的时候 TextBox txt = (TextBox)row.FindControl("txtType");
txt的值是空的?
[/Quote]

你这么问很难回答你

txt值空你有没有给这个输入框输入值?

TextBox txt = row.FindControl("txtType") as TextBox ;
if(txt!=null)
{

}
newty_won 2008-11-14
  • 打赏
  • 举报
回复
<asp:TemplateField HeaderText="网址类型" SortExpression="T_id">
<EditItemTemplate>
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2"
DataTextField="T_type" DataValueField="T_id">
</asp:DropDownList> 
<asp:TextBox ID="txtType" runat="server" Width="75px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" CommandName="Add" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" Text="添加类型" Width="61px" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# GetWebSietType(Eval("U_id").ToString(),Eval("T_id").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Jack_xiao 2008-11-14
  • 打赏
  • 举报
回复
另外,你的代码写在什么事件中了?
Jack_xiao 2008-11-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 newty_won 的回复:]
请问下 为什么
调试的时候 TextBox txt = (TextBox)row.FindControl("txtType");
txt的值是空的?
[/Quote]
GridView的前台怎么写的?代码贴出来
Jack_xiao 2008-11-14
  • 打赏
  • 举报
回复
GridView中可以设置ButtonField的CommandName属性
只要CommandName="Add",
这样CommandArgument的值会等于记录所在的行数,在RowCommand事件中可以取到

if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString())
lz是想判断当前行,在当前行插入数据?
直接插就可以了,不用循环的
newty_won 2008-11-14
  • 打赏
  • 举报
回复
请问下 为什么
调试的时候 TextBox txt = (TextBox)row.FindControl("txtType");
txt的值是空的?
criedshy 2008-11-14
  • 打赏
  • 举报
回复

刚才看错代码了:



<asp:TemplateField HeaderText="添加">
<ItemTemplate>
<asp:Button ID="btnAdd" Text="添加" CssClass="btn_long" CommandName="Add" CommandArgument="<%#DataBinder.Eval(Container.DataItem,"F_FITTING_ID")%>" runat="server" />
</ItemTemplate>
</asp:TemplateField>


其中F_FITTING_ID是你GridView1的属性DataKeyNames 设置的字段


[Quote=引用 7 楼 newty_won 的回复:]
我设置了断点,发现this.GridView1.DataKeys[row.RowIndex].Value.ToString() 的值是从1开始,随着循环递增,但是 e.CommandArgument的值呢就是“”,空的~ 因此呢 if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString())
这个条件永远都不会成立,那么要使之成立,怎么办?在button的CommandArgument属性值怎么设置?
[/Quote]
criedshy 2008-11-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 newty_won 的回复:]
我设置了断点,发现this.GridView1.DataKeys[row.RowIndex].Value.ToString() 的值是从1开始,随着循环递增,但是 e.CommandArgument的值呢就是“”,空的~ 因此呢 if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString())
这个条件永远都不会成立,那么要使之成立,怎么办?在button的CommandArgument属性值怎么设置?
[/Quote]

          <asp:TemplateField HeaderText="添加">
<ItemTemplate>
<asp:Button ID="btnAdd" Text="添加" CssClass="btn_long" CommandName="Add" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
dd__dd 2008-11-14
  • 打赏
  • 举报
回复
你要实现什么功能?还有你最开始的代码是写在什么方法或时间里的?
newty_won 2008-11-14
  • 打赏
  • 举报
回复
我设置了断点,发现this.GridView1.DataKeys[row.RowIndex].Value.ToString() 的值是从1开始,随着循环递增,但是 e.CommandArgument的值呢就是“”,空的~ 因此呢 if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString())
这个条件永远都不会成立,那么要使之成立,怎么办?在button的CommandArgument属性值怎么设置?
newty_won 2008-11-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhangzhicong 的回复:]
好象是这样的:
假如你在gridview里设置了一个
<asp:TemplateField HeaderText="增加" ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="add"
Text="增加" />
</ItemTemplate>
</asp:TemplateField>
就是上面红色加下划线的CommandName="add",用于判断是是那个按钮…
[/Quote]
我设置了断点,发现this.GridView1.DataKeys[row.RowIndex].Value.ToString() 的值是从1开始,随着循环递增,但是 e.CommandArgument的值呢就是“”,空的~ 因此呢 if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString())
这个条件永远都不会成立,那么要使之成立,怎么办?在button的CommandArgument属性值怎么设置?
newty_won 2008-11-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhangzhicong 的回复:]
不知道我有没有理解错你的问题。
[/Quote]

我设置了断点,发现this.GridView1.DataKeys[row.RowIndex].Value.ToString() 的值是从1开始,随着循环递增,但是 e.CommandArgument的值呢就是“”,空的~ 因此呢 if (this.GridView1.DataKeys[row.RowIndex].Value.ToString() == e.CommandArgument.ToString())
这个条件永远都不会成立,那么要使之成立,怎么办?在button的CommandArgument属性值怎么设置?
lxl_sprots 2008-11-14
  • 打赏
  • 举报
回复
你把你的意思 表达清楚点。。。你想要什么样的结果,这样我们才可以帮到你啊。。。
zhangzhicong 2008-11-14
  • 打赏
  • 举报
回复
不知道我有没有理解错你的问题。
zhangzhicong 2008-11-14
  • 打赏
  • 举报
回复
好象是这样的:
假如你在gridview里设置了一个
<asp:TemplateField HeaderText="增加" ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="add"
Text="增加" />
</ItemTemplate>
</asp:TemplateField>
就是上面红色加下划线的CommandName="add",用于判断是是那个按钮的,我的理解。
Leo_0924 2008-11-14
  • 打赏
  • 举报
回复
e.CommandArgument == 前台CommandArgument 指定值

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧