在command事件里获取gridview选中行某一列(ID列)的值
2个回答
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>");
}
}
}
请仔细看完,不懂请再提问。
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也可以改成别的整数 ...................... .....................}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询