如何区分取gridview的CommandArgument的值?请高手赐教!

cheng525jj 2012-11-22 05:22:31
GridView
-----------------------------------------------------------
<asp:GridView ID="AssetsFileGV" runat="server" AutoGenerateColumns="False"
OnRowCommand="AssetsFileGV_RowCommand" CellPadding="4" ForeColor="#333333"
GridLines="None" Width=99% onrowdatabound="AssetsFileGV_RowDataBound"
AllowPaging="True" onpageindexchanging="AssetsFileGV_PageIndexChanging"
PageSize="2">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField HeaderText="资产编号" DataField="AssetsRkey">
<ControlStyle Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="名称" DataField="AssetsName">
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="资产规格" DataField="AssetsSpec">
<ControlStyle Width="200px" />
</asp:BoundField>
<asp:BoundField HeaderText="购置日期" DataField="AssetsDate">
<ControlStyle Width="60px" />
</asp:BoundField>
<asp:BoundField HeaderText="责任用户" DataField="empName">
<ControlStyle Width="60px" />

</asp:BoundField>
<asp:BoundField HeaderText="责任部门" DataField="deptName">
<ControlStyle Width="60px" />
</asp:BoundField>
<asp:BoundField HeaderText="供应商" DataField="supName">
<ControlStyle Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="创建者" DataField="UserName">
<ControlStyle Width="60px" />
</asp:BoundField>
<asp:BoundField HeaderText="创建日期" DataField="AssetsCreateDate">
<ControlStyle Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="更新者" DataField="un">
<ControlStyle Width="60px" />
</asp:BoundField>
<asp:BoundField HeaderText="更新日期" DataField="AssetsUpdateDate">
<ControlStyle Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="备注" DataField="AssetsRemark">
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:TemplateField HeaderText="操作" ControlStyle-Width="50">
<ItemTemplate>
<table width="50" border="0">
<tr>
<td width="25">
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("AssetsRkey")+"%"+Eval("AssetsName")%>'
CommandName="Upd"><img src="../Images/Edit.gif" alt="编辑" border="0" /></asp:LinkButton>
</td>
<td width="25">
<asp:LinkButton ID="LinkButton3" runat="server" CommandArgument= '<%# Eval("AssetsRkey")+"%"+Eval("AssetsName")%>'
CommandName="Del" ><img src="../Images/Delete.gif" alt="删除" border="0" /></asp:LinkButton>

</td>
</tr>
</table>
</ItemTemplate>
<ControlStyle Width="50px"></ControlStyle>
</asp:TemplateField>
</Columns>
<PagerTemplate>

<asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
<asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
CommandName="Page" CommandArgument="First"></asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
CommandName="Page" CommandArgument="Prev"></asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
CommandName="Page" CommandArgument="Next"></asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
CommandName="Page" CommandArgument="Last"></asp:LinkButton>
到第<asp:TextBox runat="server" ID="inPageNum" Width="30"></asp:TextBox>页
<asp:Button ID="Button1" Text="Go" CommandName="go" runat="server" />
<br />
</PagerTemplate>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
-------------------------------------------
对应的事件:
protected void AssetsFileGV_RowCommand(object sender, GridViewCommandEventArgs e)
{
//string dw = e.CommandArgument.ToString();
string[] estr = e.CommandArgument.ToString().Split('%');
string key = estr[0].ToString().Trim();
string NameInfo = estr[1].ToString().Trim();
cf = new Config();
if (e.CommandName == "Del")
{
/***********************删除信息*****************************/
//if (gb.get_right(AppCommon.userrkey, "AllContactLists", "Del") == true)
//{
strsql = "delete from AssetsInfo where AssetsRkey='" + key + "' and AssetsName ='" + NameInfo + "'";
cf.ExecuteNonQuery(strsql); //调用类中方法执行SQL,删除一条记录.
//}
//else
// cf.Alert("Sorry,你无删除权限!", Page);
}

if (e.CommandName == "Upd")
{
//if (gb.get_right(AppCommon.userrkey, "AllContactLists", "Edit") == true)
//{
Response.Redirect("AddItAssets.aspx?opt=edit&url=ItAssetsList&AssetsRkey='" + key + "'&AssetsName = '" + NameInfo + "'");
//Response.Write("AddItAssets.aspx?opt=edit&url=ItAssetsList&AssetsRkey='" + key + "'&AssetsName = '" + NameInfo + "'");
//Response.End();
//}
//else
// cf.Alert("Sorry,你无编辑权限!", Page);
}
if (e.CommandName == "go")
{
try
{
TextBox tb = (TextBox)this.AssetsFileGV.BottomPagerRow.FindControl("inPageNum");
int num = Int32.Parse(tb.Text);
GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
AssetsFileGV_PageIndexChanging(null, ea);
}
catch
{
}
}
BindGV();
}
结果点击换页时,报错:

索引超出了数组界限。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限。
报错的行:
行 76: string[] estr = e.CommandArgument.ToString().Split('%');
行 77: string key = estr[0].ToString().Trim();
行 78: string NameInfo = estr[1].ToString().Trim();

...全文
164 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lee3217813 2012-11-23
  • 打赏
  • 举报
回复
或者去掉分页按钮的CommandArgument,和CommandName属性
lee3217813 2012-11-23
  • 打赏
  • 举报
回复
你把分页的放到gridview外面啊。
紫魂一号 2012-11-23
  • 打赏
  • 举报
回复
每个linkbutton 都有自己的事件哈。。

protected void LinkButton_Command(object sender, CommandEventArgs e)
{
    string arg = e.CommandArgument.ToString();//这个是绑定的参数值
    string command = e.CommandName;//用于判断。任何linkbutton 都可以调用此事件,用这个参数加以区分
     //利用这两个参数可以做处理
}
cheng525jj 2012-11-23
  • 打赏
  • 举报
回复
string[] estr能取到数据。 <asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next"></asp:LinkButton> 我点击分页时,居然取linkButton的CommandArgument的信息"Next".
耗子哭死猫 2012-11-22
  • 打赏
  • 举报
回复
你看看你string[] estr 这个数组; estr[1]能取到值吗
aluogang 2012-11-22
  • 打赏
  • 举报
回复
试试用AspNetPager做分页吧

62,244

社区成员

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

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

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

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