在我的代码隐藏中,我得到的公司名称和地址为:
connection.Open();
sqlCmd = new SqlCommand(sqlCmd.CommandText, connection);
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
DataTable dt = new DataTable();
sqlReader.Read();
dt.Load(sqlReader);
for(int i>=0 ; i = dt.Rows.Cout - 1; i--)
{
string companyName[i] = dt.Rows[i]["companyName"].ToString();
sqlCmd.CommandText = addr;
sqlCmd.Parameters.AddWithValue("companyName", dt.Rows[i]["companyName"].ToString());
using (var addressReader = sqlCmd.ExecuteReader())
{
if (addressReader.Read())
{
string address[i] = addressReader["address"].ToString();
}
}
}
所以我想在我的ascx文件中放入companyName [i]和address [i]值。目前它使用的是datagrid dgcompanies,但我不想使用datagrid,只想重用当前的表格格式:
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr id="resultsRow" runat="server">
<td vAlign="top">
<a name="tagCompaniesDatagrid"></a>
<asp:datagrid id="dgCompanies" Width="100%" AllowSorting="True" DataKeyField="companyId" AutoGenerateColumns="False"
AllowPaging="True" AllowCustomPaging="True" OnPageIndexChanged="dgCompanies_Paging" Runat="server" onprerender="dgCompanies_PreRender" >
<HeaderStyle CssClass="DGColumnHead" />
<ItemStyle CssClass="DGItemStyle" />
<AlternatingItemStyle CssClass="DGAlternatingItemStyle" />
<FooterStyle CssClass="DGColumnFoot" />
<PagerStyle CssClass="DGPagerStyle" HorizontalAlign="Right" Mode="NumericPages" PageButtonCount="5" />
<Columns>
<asp:TemplateColumn SortExpression="companyName" HeaderText="Company Name">
<HeaderStyle Width="40%" />
<ItemTemplate>
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td width="95%">
<asp:LinkButton CssClass="LinkButton" id="btnView" Runat="server" CommandName="ViewDetails" CommandArgument="<%# GetViewUrl((System.Data.Common.DbDataRecord)Container.DataItem) %>">
<span ID="SpanTitle" Runat="server">
<%# DataBinder.Eval(Container.DataItem,"companyName") %>
</span>
</asp:LinkButton>
<asp:Label CssClass="DGNormal" ID="lblStatus" Runat="server" text='<%# StatusToText((int) DataBinder.Eval(Container.DataItem,"statusId")) %>'/>
<asp:Image visible='<%# (bool) DataBinder.Eval(Container.DataItem,"WMCCMRated") %>' ID="imgProfiled" Runat="server" ImageUrl="~/images/WMCCMRated.gif" AlternateText="This company has been competency profiled by WMG" />
<asp:Image Visible='<%# !(Convert.IsDBNull(DataBinder.Eval(Container.DataItem,"feedback")))%>' ID="feedback" Runat="server" ImageUrl="~/images/feedback.gif" AlternateText="The number of feedback this company has been received is ->" ImageAlign="Bottom" Height="18px" Width="12px"/>
<asp:Label CssClass="NormalBold" ID="lblFeedback" Runat="server" text='<%# DataBinder.Eval(Container.DataItem,"feedback") %>' ForeColor="Navy"/>
</td>
</tr>
<tr>
<td>
<span class="DGNormalBold">
<%# DataBinder.Eval(Container.DataItem,"phoneNumber") %>
</span>
</td>
</tr>
<tr>
<td>
<span class="DGNormal" ID="SpanProfile" Runat="server">
<%# cutToNCharacters((string)DataBinder.Eval(Container.DataItem,"Profile")) %>
</span>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<cc1:DropDownCombo ID="dplCompanies" Runat="server" Width="95%" DataTextField="CompanyName" DataValueField="CompanyId" DataSource='<%# GetCompanyNames() %>' />
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="address" HeaderText="Address">
<HeaderStyle Width="15%" />
<ItemTemplate>
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td>
<span class="DGNormal" ID="SpanAddress" Runat="server">
<%# DataBinder.Eval(Container.DataItem,"address")%>
</span>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
我的理解是,我应该替换这两行:
<%# DataBinder.Eval(Container.DataItem,"companyName") %>
和
<%# DataBinder.Eval(Container.DataItem,"address")%>
但我不知道还需要改变什么,以及如何让它自动填充我的一组值。有谁熟悉这个?
答案 0 :(得分:0)
聊天结束后,这就是你需要的......
数据需要存储在模型中,然后才能“回显”到页面上。您需要根据需要修改模型......
<table>
<tr>
<td>company</td><td>address</td>
</tr>
<asp:Repeater ID="companyRepeater" runat="server">
<ItemTemplate>
<tr>
<td><%#Eval("companyName") %></td><td><%#Eval("address") %></td>
</tr>
</Itemtemplate>
</asp:Repeater>
</table>
public class CompanyModel{
public string companyName {get; set;}
public string address {get; set;}
}
sqlCmd = new SqlCommand("SELECT companyName, address FROM company_table", connection);
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
DataTable dt = new DataTable();
List<CompanyModel> companies= new List<CompanyModel>();
sqlReader.Read();
dt.Load(sqlReader);
for(int i = 0; i < dt.Rows.Count; i++)
{
companies.Add(new CompanyModel
{
companyName = dt.Rows[i]["companyName"].ToString(),
address = dt.Rows[i]["address"].ToString()
});
}
companyRepeater.DataSource = companies;
companyRepeater.DataBind();