Gridview模板字段数据源 [英] Gridview template field datasource
问题描述
在gridview中,有两个字段(电话,类型)需要是模板字段而不是绑定字段,在编辑期间,和项目模板中的文本框或标签(无论哪个)。
当我运行代码时,datagrid完成并填写 - 减去手机或类型字段(模板字段)的信息。
如何数据绑定字段,以便信息正常显示。
我已经包括我的代码。
任何帮助将不胜感激。感谢提前!
GRIDVIEW:
< asp:GridView ID =gvMTMrunat =serverAllowPaging =TrueDataKeyNames =MTMID
AutoGenerateColumns =FalseAutoGenerateEditButton =True
OnRowEditing =gvMTM_RowEditing
OnRowUpdating = gvMTM_UpdateRowEditing
OnRowCancelingEdit =gvMTM_CancelRowEditing>
<列>
< asp:BoundField DataField =MTMIDHeaderText =ID/>
< asp:BoundField DataField =MTMNameHeaderText =Name/>
< asp:BoundField DataField =Add1HeaderText =Add1/>
< asp:BoundField DataField =Add2HeaderText =Add2/>
< asp:BoundField DataField =CityHeaderText =City/>
< asp:BoundField DataField =STHeaderText =ST/>
< asp:BoundField DataField =ZipHeaderText =Zip/>
< asp:BoundField DataField =EmailHeaderText =Email/>
< asp:TemplateField HeaderText =Phone#>
< EditItemTemplate>
< asp:TextBox ID =txtPhoneEditrunat =server>< / asp:TextBox>
< asp:MaskedEditExtender ID =TemptxtPhone_MaskedEditExtenderrunat =server
CultureAMPMPlaceholder =CultureCurrencySymbolPlaceholder =
CultureDateFormat =CultureDatePlaceholder =CultureDecimalPlaceholder =
CultureThousandsPlaceholder =CultureTimePlaceholder =Enabled =True
TargetControlID =TemptxtPhone>
< / asp:MaskedEditExtender>
< / EditItemTemplate>
< ItemTemplate>
< asp:TextBox ID =txtPhoneReadrunat =serverBorderStyle =None>< / asp:TextBox>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =RR>
< EditItemTemplate>
< asp:DropDownList ID =ddRREditrunat =serverHeight =16pxWidth =100pxDataTextField =RailroadDataValueField =RRID>
< / asp:DropDownList>
< / EditItemTemplate>
< ItemTemplate>
< asp:TextBox ID =txtRRReadrunat =serverBorderStyle =None>< / asp:TextBox>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< / asp:GridView>
on .CS页面:
gvMTM.DataSource = code.GetMTMList();
gvMTM.DataBind();
SQL调用
code> public DataTable GetMTMList()
{
try
{
SQLCON = new SqlConnection(connectionString);
SQLCON.Open();
SQLCmd = new SqlCommand(spGetMTMList,SQLCON);
SQLCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(SQLCmd);
DataTable Detailtable = new DataTable();
adapter.Fill(Detailtable);
return Detailtable;
}
catch(Exception ex)
{
HttpContext.Current.Response.Redirect(〜/ ErrorRedirect.aspx?+ ex.Message,false) ;
返回null;
}
finally
{
SQLCON.Close();
}
}
使用这是:
Text ='<%#Eval(phone)%>'
您需要添加以下内容:
< ItemTemplate>
< asp:TextBox ID =txtPhoneReadrunat =serverText ='<%#Eval(phone)%>'>
< / asp:TextBox>
< / ItemTemplate>
Eval()
方法绑定数据您的数据源中的 Text
属性
您可以查看他的邮箱了解更多信息
注意
请考虑在< ItemTemplate>
TextBox >到 asp:Label
应该如下所示:
< ItemTemplate>
< asp:Label ID =txtPhoneReadrunat =serverText ='<%#Eval(Phone)%>'>
< / asp:Label>
< / ItemTemplate>
I've placed a gridview on my asp.net page, and am using a sql call to obtain the datasource. In the sql call, it brings the data for all the fields(name, address, phone, type) On the gridview, there are 2 fields (phone, type) that need to be template fields rather than bound field, during edit, and textboxes or labels (whichever) in the Item template.
When I run the code, the datagrid completes and fills in - minus the information for the phone or type fields (template fields). How do I databind the fields so the information will show up properly. I've included my code.
Any help would be greatly appreciated. Thanks in advance!
GRIDVIEW:
<asp:GridView ID="gvMTM" runat="server" AllowPaging="True" DataKeyNames="MTMID"
AutoGenerateColumns="False" AutoGenerateEditButton="True"
OnRowEditing="gvMTM_RowEditing"
OnRowUpdating="gvMTM_UpdateRowEditing"
OnRowCancelingEdit="gvMTM_CancelRowEditing" >
<Columns>
<asp:BoundField DataField="MTMID" HeaderText="ID" />
<asp:BoundField DataField="MTMName" HeaderText="Name" />
<asp:BoundField DataField="Add1" HeaderText="Add1" />
<asp:BoundField DataField="Add2" HeaderText="Add2" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="ST" HeaderText="ST" />
<asp:BoundField DataField="Zip" HeaderText="Zip" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:TemplateField HeaderText="Phone#">
<EditItemTemplate>
<asp:TextBox ID="txtPhoneEdit" runat="server"></asp:TextBox>
<asp:MaskedEditExtender ID="TemptxtPhone_MaskedEditExtender" runat="server"
CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder=""
CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder=""
CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"
TargetControlID="TemptxtPhone">
</asp:MaskedEditExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtPhoneRead" runat="server" BorderStyle="None"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RR">
<EditItemTemplate>
<asp:DropDownList ID="ddRREdit" runat="server" Height="16px" Width="100px" DataTextField="Railroad" DataValueField="RRID">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtRRRead" runat="server" BorderStyle="None"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
on .CS page:
gvMTM.DataSource = code.GetMTMList();
gvMTM.DataBind();
SQL call
public DataTable GetMTMList()
{
try
{
SQLCON = new SqlConnection(connectionString);
SQLCON.Open();
SQLCmd = new SqlCommand("spGetMTMList", SQLCON);
SQLCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(SQLCmd);
DataTable Detailtable = new DataTable();
adapter.Fill(Detailtable);
return Detailtable;
}
catch (Exception ex)
{
HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx?" + ex.Message, false);
return null;
}
finally
{
SQLCON.Close();
}
}
Use this:
Text='<%# Eval("phone") %>'
You need to add this:
<ItemTemplate>
<asp:TextBox ID="txtPhoneRead" runat="server" Text='<%# Eval("phone") %>'>
</asp:TextBox>
</ItemTemplate>
The Eval()
method bind the data inside your data source to the Text
property
You can take a look at his post for more information
Note
Consider changing the TextBox
control within the <ItemTemplate>
to asp:Label
it should look like this:
<ItemTemplate>
<asp:Label ID="txtPhoneRead" runat="server" Text='<%# Eval("Phone") %>'>
</asp:Label>
</ItemTemplate>
这篇关于Gridview模板字段数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!