1

我在我的 asp.net 页面上放置了一个 gridview,并且正在使用 sql 调用来获取数据源。在sql调用中,它带来了所有字段(姓名,地址,电话,类型)的数据在gridview上,有2个字段(电话,类型)需要模板字段而不是绑定字段,在编辑过程中,和项目模板中的文本框或标签(无论哪个)。

当我运行代码时,数据网格完成并填写 - 减去电话或类型字段(模板字段)的信息。如何对字段进行数据绑定,以便正确显示信息。我已经包含了我的代码。

任何帮助将不胜感激。提前致谢!

网格视图:

  <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>

在 .CS 页面上:

     gvMTM.DataSource = code.GetMTMList();
            gvMTM.DataBind();

SQL 调用

    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();
        }
    }
4

1 回答 1

10

用这个:

Text='<%# Eval("phone") %>'

你需要添加这个:

 <ItemTemplate>
      <asp:TextBox ID="txtPhoneRead" runat="server" Text='<%# Eval("phone") %>'>
      </asp:TextBox>
 </ItemTemplate>

Eval()方法将数据源中的数据绑定到Text属性

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>
于 2013-03-18T19:15:30.170 回答