GridView 获取当前行中数据的ID
如题,我要做一个收件箱邮件列表!就是在GridView多选的情况下删除选中行!数据库的字段为:邮件ID(WS_MAID),主题(WS_MailObject),发件人(WS...
如题,我要做一个收件箱邮件列表!就是在GridView多选的情况下删除选中行!
数据库的字段为:邮件ID(WS_MAID),主题(WS_MailObject),发件人(WS_MailSendUid),日期(WS_MailSendDate)
GridView源码如下:
<asp:GridView ID="GridView1 runat="server" AutoGenerateColumns="False"
Height="69px" Width="402px">
<Columns>
<asp:HyperLinkField DataTextField="WS_MailObject" HeaderText="主题" />
<asp:BoundField DataField="WS_Name" HeaderText="收件人" />
<asp:BoundField DataField="WS_MailSendDate" HeaderText="日期" />
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<asp:CheckBox ID="xuanze" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="quanxuan" runat="server" Text="全选" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
想在 多选情况下 提交删除,
这是全选代码:
protected void quanxuan_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox ch=(CheckBox)gr.Cells[4.FindControl("xuanze");
if (!ch.Checked)
{
ch.Checked = true;
}
else
{
ch.Checked = false;
}
}
}
我想请教的是在如下多选情况下如何获取选中的邮件ID值!
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox ch = (CheckBox)gr.Cells[5].FindControl("xuanze");
if (ch.Checked)
{
Response.Write(邮件ID值);//邮件ID即为 WS_MAID,如何得到这个WS_MAID
}
}
本人刚开始学.net,啥都不懂!自学很痛苦,请大家不吝赐教啊!!!谢谢了! 展开
数据库的字段为:邮件ID(WS_MAID),主题(WS_MailObject),发件人(WS_MailSendUid),日期(WS_MailSendDate)
GridView源码如下:
<asp:GridView ID="GridView1 runat="server" AutoGenerateColumns="False"
Height="69px" Width="402px">
<Columns>
<asp:HyperLinkField DataTextField="WS_MailObject" HeaderText="主题" />
<asp:BoundField DataField="WS_Name" HeaderText="收件人" />
<asp:BoundField DataField="WS_MailSendDate" HeaderText="日期" />
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<asp:CheckBox ID="xuanze" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="quanxuan" runat="server" Text="全选" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
想在 多选情况下 提交删除,
这是全选代码:
protected void quanxuan_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox ch=(CheckBox)gr.Cells[4.FindControl("xuanze");
if (!ch.Checked)
{
ch.Checked = true;
}
else
{
ch.Checked = false;
}
}
}
我想请教的是在如下多选情况下如何获取选中的邮件ID值!
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox ch = (CheckBox)gr.Cells[5].FindControl("xuanze");
if (ch.Checked)
{
Response.Write(邮件ID值);//邮件ID即为 WS_MAID,如何得到这个WS_MAID
}
}
本人刚开始学.net,啥都不懂!自学很痛苦,请大家不吝赐教啊!!!谢谢了! 展开
3个回答
展开全部
<asp:GridView ID="GridView1 runat="server" AutoGenerateColumns="False"
Height="69px" Width="402px"
-- 注意这里 ------------
DataKeyNames="WS_MAID" >
---------------------
<Columns>
<asp:HyperLinkField DataTextField="WS_MailObject" HeaderText="主题" />
<asp:BoundField DataField="WS_Name" HeaderText="收件人" />
<asp:BoundField DataField="WS_MailSendDate" HeaderText="日期" />
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<asp:CheckBox ID="xuanze" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="quanxuan" runat="server" Text="全选" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代码:
for(int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
{
CheckBox ch = (CheckBox)GridView1.Rows[rowIndex].Cells[5].FindControl("xuanze");
if (ch.Checked)
{
string 邮件ID值 = GridView1.DataKeys[rowIndex].Value.ToString();
Response.Write(邮件ID值);//邮件ID即为 WS_MAID,如何得到这个WS_MAID
}
}
试一下吧,有问题留言
Height="69px" Width="402px"
-- 注意这里 ------------
DataKeyNames="WS_MAID" >
---------------------
<Columns>
<asp:HyperLinkField DataTextField="WS_MailObject" HeaderText="主题" />
<asp:BoundField DataField="WS_Name" HeaderText="收件人" />
<asp:BoundField DataField="WS_MailSendDate" HeaderText="日期" />
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<asp:CheckBox ID="xuanze" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="quanxuan" runat="server" Text="全选" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代码:
for(int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
{
CheckBox ch = (CheckBox)GridView1.Rows[rowIndex].Cells[5].FindControl("xuanze");
if (ch.Checked)
{
string 邮件ID值 = GridView1.DataKeys[rowIndex].Value.ToString();
Response.Write(邮件ID值);//邮件ID即为 WS_MAID,如何得到这个WS_MAID
}
}
试一下吧,有问题留言
展开全部
可以在GridView1上添加一个隐藏的ID列,然后取这列的值。要注意的是隐藏列不能使用Visible="False",否则取不到值。隐藏的方法是在GridView1_RowDataBound事件中使用Style["display"] = "none ";
ASPX代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Height="69px" Width="402px" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:HyperLinkField DataTextField="WS_MailObject" HeaderText="主题" />
<asp:BoundField DataField="WS_Name" HeaderText="收件人" />
<asp:BoundField DataField="WS_MailSendDate" HeaderText="日期" />
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<asp:CheckBox ID="xuanze" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="quanxuan" runat="server" Text="全选" />
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
</Columns>
</asp:GridView>
C#:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[4].Style["display"] = "none ";
}
然后取值就简单了:
Response.Write(gr.Cells[4].Text.ToString());//邮件ID即为 WS_MAID,如何得到这个WS_MAID
ASPX代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Height="69px" Width="402px" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:HyperLinkField DataTextField="WS_MailObject" HeaderText="主题" />
<asp:BoundField DataField="WS_Name" HeaderText="收件人" />
<asp:BoundField DataField="WS_MailSendDate" HeaderText="日期" />
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<asp:CheckBox ID="xuanze" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="quanxuan" runat="server" Text="全选" />
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
</Columns>
</asp:GridView>
C#:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[4].Style["display"] = "none ";
}
然后取值就简单了:
Response.Write(gr.Cells[4].Text.ToString());//邮件ID即为 WS_MAID,如何得到这个WS_MAID
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你参考一下吧,,基本跟你所说的一样..
记住要在gridview中的datakeynames中设定为ID字段
这句话就是获取当前ID gdvBiddingNotice.DataKeys[rows.RowIndex].Value.ToString()
foreach (GridViewRow rows in gdvBiddingNotice.Rows)
{
CheckBox chkSelected = (CheckBox)rows.Cells[0].FindControl("CheckBox1");
if (chkSelected.Checked)
{
int result = noticebll.DeleteBiddingNotice(Convert.ToInt32(gdvBiddingNotice.DataKeys[rows.RowIndex].Value.ToString()));
}
}
记住要在gridview中的datakeynames中设定为ID字段
这句话就是获取当前ID gdvBiddingNotice.DataKeys[rows.RowIndex].Value.ToString()
foreach (GridViewRow rows in gdvBiddingNotice.Rows)
{
CheckBox chkSelected = (CheckBox)rows.Cells[0].FindControl("CheckBox1");
if (chkSelected.Checked)
{
int result = noticebll.DeleteBiddingNotice(Convert.ToInt32(gdvBiddingNotice.DataKeys[rows.RowIndex].Value.ToString()));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询