我正在创建该线程,以作为上一个here的后续操作。
我正在尝试创建一个gridview,它填充了数据库中的一些数据。但是,从底部3个边界域可以看出,我阻止了它们出现在我的Web应用程序中。
<Columns>
<asp:BoundField DataField="memberreportid" HeaderText="property" SortExpression="false"/>
<asp:BoundField DataField="typeofcrime" HeaderText="property" SortExpression="false" />
<asp:BoundField DataField="crdatetime" HeaderText="property" SortExpression="false" />
<asp:BoundField DataField="address" HeaderText="property" SortExpression="false" />
<asp:BoundField DataField="detail" HeaderText="property" SortExpression="false"/>
<asp:BoundField DataField="incidentdate" HeaderText="property" SortExpression="false" />
<asp:BoundField DataField="incidenttime" HeaderText="victim" SortExpression="false"/>
<asp:BoundField DataField="property" HeaderText="suspect" SortExpression="false" Visible="false" />
<asp:BoundField DataField="victim" HeaderText="suspect" SortExpression="false" Visible="false" />
<asp:BoundField DataField="suspect" HeaderText="suspect" SortExpression="false" Visible="false" />
</Columns>
尽管它们无法通过网页实际看到,我仍试图将它们显示在标签上。单击选择按钮时,我使用了这种方法来显示它们(我将“ AutoGenerateSelectColumn”设置为true)
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
lbmemberreportid.Text = GWCase.SelectedRow.Cells[1].Text;
lblproperty.Text = GWCase.SelectedRow.Cells[8].Text;
lblvictim.Text = GWCase.SelectedRow.Cells[9].Text;
lblsuspect.Text = GWCase.SelectedRow.Cells[10].Text;
}
[不幸的是,我只能在标签上显示memberreportID,但不能显示其他3个服装,我相信这不是因为我物理上隐藏了它。除了在gridview中被选中时试图显示特定值的方法以外,还有其他方法吗?
已更新
Page_load绑定
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGrid();
}
}
private void LoadGrid()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT memberreportid, typeofcrime, crdatetime, address, detail, incidentdate, incidenttime, property, victim, suspect, detail, suspectdetail, propertydetail from memberreport", conn);
da.Fill(ds);
GWCase.DataSource = ds.Copy();
GWCase.DataBind();
conn.Close();
ddlpid1.Visible = false;
ddlpid2.Visible = false;
ddlpid3.Visible = false;
ddlpid4.Visible = false;
ddlpid5.Visible = false;
ddlpid6.Visible = false;
ddlpid7.Visible = false;
ddlpid8.Visible = false;
ddlpid9.Visible = false;
ddlpid10.Visible = false;
}
试图读取隐藏字段
protected void GWCase_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
LoadGrid();
GWCase.PageIndex = e.NewPageIndex;
GWCase.DataBind();
}
您可以采用其他方法,然后将值放在隐藏字段中:
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdfSuspect" runat="server" Value='<%# Eval("suspect") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
然后您可以通过执行以下操作来访问它:
protected void GWCase_SelectedIndexChanged(object sender, EventArgs e)
{
...
lblsuspect.Text = ((HiddenField) GWCase.SelectedRow.FindControl("hdfSuspect")).Value;
}
您需要使用GridView的属性:DataKeyNames
或使用FindControl()
时使用TemplateFields
方法
如果使用DataKeyNames
:
Setdatakeynames= "memberreportid"
//仅使用主键。可以使用多个字段
您可以在SelectedIndexchanged事件中访问与[gridView1.DataKeys[gridView1.SelectedIndex].Value.ToString();
]相同的对象>
请参阅this link,以了解有关读取隐藏列值的不同方式的完整说明。
这是如何在标签中显示网格的值