GRIDVIEW里就一个列,定义如下: DataTable dt = new DataTable();
dt.Columns.Add("name");
DataRow row = dt.NewRow();
dt.Rows.Add("aaa");
dt.Rows.Add("bbb");
dt.Rows.Add("ccc");
this.GridView1.DataSource = dt;
this.GridView1.DataBind();然后我又定义了一个删除列,想通过按删除把前面这列的内容删掉
可是现在在RowDeleting事件中我取到的却是空值,请问为什么? string name = this.GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();name为空,如何解决?我想达到的效果是点删除即删除该行
dt.Columns.Add("name");
DataRow row = dt.NewRow();
dt.Rows.Add("aaa");
dt.Rows.Add("bbb");
dt.Rows.Add("ccc");
this.GridView1.DataSource = dt;
this.GridView1.DataBind();然后我又定义了一个删除列,想通过按删除把前面这列的内容删掉
可是现在在RowDeleting事件中我取到的却是空值,请问为什么? string name = this.GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();name为空,如何解决?我想达到的效果是点删除即删除该行
解决方案 »
- 关于从数据库中取IP数据的问题!
- 求高手反编译
- 请问关于自动登陆
- 求一个经典算法
- AJAX设置DIV的innerhtml的问题
- 急!地球人都知道的一个简单的打印命令。
- 有ASP基础,如何向ASP.NET转?
- MVC4发布到jexus上报System.TypeLoadException异常,求解决
- 奇怪:我的VS.NET今天突然不能用了.出了这错,大家帮我看看,谢谢了.
- 有没有好的iframe替代方案,ajax加载有问题(解决速结贴)
- asp.net2.0 能不能捕捉所有异常并保存到一个文件里?
- using(Class1 c = new Class1()){ c.Print(); },请问程序里用到using,必须要实现接口System.IDisposable吗?
string name=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:alert('当前ID为:" +DataBinder.Eval(e.Row.DataItem,"CID") + "')");
}
}
我做的是点击当前行获取行记录的ID,不过GRIDVIEW的DATAKEYSNAME要设成ID。。
然后获取GridView1.DataKeys[e.RowIndex].Values[0]获取主键
<ItemStyle Width="100px" />
</asp:CommandField>后台:RowDeleting 事件中: string sqllist = "delete from table where code ='" + GridList.DataKeys[e.RowIndex].Value.ToString() + "'";
cmd.Delete(sqllist);
////////////////////
html 代码
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None"
Width="100%" onpageindexchanging="GridView1_PageIndexChanging"
onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField HeaderText="广告id" DataField="Adid" />
<asp:BoundField DataField="Wid" HeaderText="用户id" />
<asp:BoundField DataField="Yid" HeaderText="订单id" />
<asp:HyperLinkField DataTextField="YTitle" HeaderText="订单标题" DataNavigateUrlFields="Yid" DataNavigateUrlFormatString="Manager/CustomerService/Orders/OrderInfo.aspx?id={0}" />
<asp:BoundField DataField="Addtime" HeaderText="订单时间" />
<asp:TemplateField HeaderText="管理">
<ItemTemplate>
<asp:LinkButton ID="lkbtnSettlement" CommandName="yes" CommandArgument='<%# Bind("ID") %>' runat="server" Text="通过" Visible="false"> </asp:LinkButton>
<asp:LinkButton ID="LinkButton1" CommandName="no" CommandArgument='<%# Bind("ID") %>' runat="server" Text="未通过" Visible="false"> </asp:LinkButton>
<asp:Label ID="lblStates" runat="server" Text='<%# Bind("ValidOrder") %>' Visible="false"></asp:Label>
<asp:Label ID="lblOk" runat="server" Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>/////////////////////////////////
cs代码 //修改事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//通过
if (e.CommandName.Equals("yes"))
{
Update(e.CommandArgument.ToString(), "0");
}
//未通过
if (e.CommandName.Equals("no"))
{
Update(e.CommandArgument.ToString(), "1");
}
}
//修改状态
private void Update(string id, string state)
{
try
{
CpaLog cpa = new CpaLog();
if (cpa.UpdState(id, state))
{
<script>alert('操作成功');</script>
}
else
{
<script>alert('操作失败');</script>
}
}
catch
{
script>alert('操作失败');</script>
}
}