如何将数据库值分配给详细信息视图项模板标签 [英] How to assign Database value to Detail view item Template label
本文介绍了如何将数据库值分配给详细信息视图项模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
您能否更正以下代码
我必须从数据库中获取价值并将其分配给Detailview项目模板标签
Hi
Could you please correct the below code
I have to take value from database and assign it to Detailview item template label
<fields> <asp:TemplateField HeaderText="Company Name">
<itemtemplate>
<asp:Label ID="complbl" Text = "Hi" runat = "server">
</itemtemplate>
<asp:TemplateField HeaderText="Company Address">
<itemtemplate>
<asp:Label ID="addlbl" runat="server" Visible="true">
</itemtemplate>
</fields>
while (reader.Read())
{
doDate = reader["DoDate"].ToString();
doNumber = reader["DoNumber"].ToString();
customerId = reader["CusId"].ToString();
comp = reader["CompName"].ToString();
DoNotb.Text = doNumber;
doDatetb.Text = doDate;
cidtb.Text = customerId;
Label compname = DetailsView1.Rows[1].Cells[0].FindControl("complbl");
comp = compname.Text;
compname.Text = comp;
}
我收到错误消息,因为"
I am getting error as "
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Thanks
Thanks
推荐答案
更正后的代码为
Corrected code as
Collapse | Copy Code
<fields> <asp:TemplateField HeaderText="Company Name">
<itemtemplate>
<asp:Label ID="complbl" Text = "Hi" runat = "server" text='<%# Eval("complbl")%>' >
</itemtemplate>
<asp:TemplateField HeaderText="Company Address">
<itemtemplate>
<asp:Label ID="addlbl" runat="server" Visible="true" text='<%# Eval("complbl")%>' >
</itemtemplate>
</fields>
<fields>
<asp:templatefield headertext="Company Name" xmlns:asp="#unknown">
<itemtemplate>
<asp:label id="complbl" Text='<%# Eval("complbl")%>' runat="server">
</itemtemplate>
<asp:templatefield headertext="Company Address">
<itemtemplate>
<asp:label id="addlbl" runat="server" Text='<%# Eval("compAddress")%>' visible="true">
</itemtemplate>
</fields>
在服务器端,您只需要将数据表分配给gridview的DataSource.
DataTable中应该有complbl,compAddress列
让我知道是否有任何疑问
如果解决了您的问题,请标记为解决方案
At Server side you just need to assign datatable to DataSource of gridview.
There should be column complbl,compAddress in DataTable
let me know if any queries
Mark as solution if its solved your problem
这篇关于如何将数据库值分配给详细信息视图项模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文