在command事件里获取gridview选中行某一列(ID列)的值

 我来答
匿名用户
2013-07-13
展开全部
在模板列里找,例如新闻(News)的删除、修改与发布操作,发布之后只能进行删除而不能进行修改。代码片段:(请注意粗体部分)<asp:GridView ID="gvNewsInfo" runat="server" AutoGenerateColumns="False" CellPadding="3"
GridLines="Horizontal" Width="90%" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
BorderWidth="1px" DataKeyNames="NewsId" OnRowCommand="gvNewsInfo_RowCommand"
OnRowDataBound="gvNewsInfo_RowDataBound">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<RowStyle BackColor="#E7E7FF" HorizontalAlign="Center" ForeColor="#4A3C8C" /><Columns><asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="lbtnUpdate" runat="server" ForeColor="Blue" CommandName="UpdateNews"
CommandArgument='<%#Eval("NewsId") %>'>修改</asp:LinkButton>|<asp:LinkButton ID="lbtnDelete"
runat="server" ForeColor="Blue" CommandName="DeleteNews" CommandArgument='<%#Eval("NewsId") %>'>删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="发布">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbtnPublish" ForeColor="Blue"
CommandName="Publish" CommandArgument='<%#Eval("NewsId") %>'>发布</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField></Columns><PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>后台代码片段 protected void gvNewsInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string newsId = gvNewsInfo.DataKeys[e.Row.RowIndex].Value.ToString();
LinkButton lbtnDelete = (LinkButton)e.Row.FindControl("lbtnDelete");
lbtnDelete.CommandArgument = newsId;
lbtnDelete.Attributes.Add("onclick", "return confirm('确定要删除吗?');");
LinkButton lbtnUpdate = (LinkButton)e.Row.FindControl("lbtnUpdate");
LinkButton lbtnPublish = (LinkButton)e.Row.FindControl("lbtnPublish");
lbtnUpdate.CommandArgument = newsId;
lbtnUpdate.PostBackUrl = "CreateNews.aspx?newsId=" + Server.UrlEncode(newsId.Trim()) + "";
string newsState = DataBinder.Eval(e.Row.DataItem, "NewsState").ToString();
if (newsState == "1")
{
lbtnUpdate.Enabled = false;
lbtnPublish.Enabled = false;
}
}
}
protected void gvNewsInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("DeleteNews"))
{
string newsId = e.CommandArgument.ToString();
bool deleteNews = NewsManager.DeleteNewsByNewsId(int.Parse(newsId));
if (deleteNews)
{
DisplayNewsInfo();
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败!')</script>");
}
}
if (e.CommandName.Equals("UpdateNews"))
{
string newsId = e.CommandArgument.ToString();
Response.Redirect("CreateNews.aspx?newsId=" + Server.UrlEncode(newsId.Trim()) + "");
} if (e.CommandName.Equals("Publish"))
{
string newsId = e.CommandArgument.ToString();
bool result = NewsManager.UpdateNewStateByNewsId(int.Parse(newsId));
if (result)
{
DisplayNewsInfo();
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert(发布失败!')</script>");
}
}
}
请仔细看完,不懂请再提问。
匿名用户
2013-07-13
展开全部
纠正个错误,不是command而是GridView.RowCommand事件,差一个字差很多。 c# asp.net 3.5,4.0都适用protected void MyGridView_RowCommand(object sender, GridViewCommandEventArgs e ){ int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = new GridViewRow(); row = MyGridView.Rows[index].Columns["这里输入你要找的列的名字"]; // index也可以改成别的整数 ...................... .....................}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式