我怎样才能获得SelectedIndexChanged来获取companyname而不是companyid到文本框,2个不同的表?

时间:2017-02-21 16:25:45

标签: c# asp.net

我有2个表,我想从中获取数据。第一个表是Contractor表,其中包含承包商个人信息,第二个表是Company表,其中包含2列,CompanyName和CompanyID。两个表都有相同的列,CompanyID。

我当前的代码有一个gridview,其中包含" Select" column,SelectedIndexChanged返回数据库中的值以填充文本框。

我想要做的是获取公司名称而不是填入文本框的公司ID(txtCompany),如何修改下面的当前代码呢?

ASPX

<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPID" runat="server"></asp:TextBox>
<asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>

<asp:GridView ID="TDetails" DataSourceID="SqlDataSource1" DataKeyNames="PersonID" AutoGenerateColumns="false" runat="server" OnSelectedIndexChanged="TDetails_SelectedIndexChanged" >
        <HeaderStyle BackColor="gray" ForeColor="White" />
        <Columns
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbtnSelect" runat="server" CommandName="Select" Text="Select" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Last Name">
                <ItemTemplate>
                    <asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="First Name">
                <ItemTemplate>
                    <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Company" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblCompany" runat="server" Text='<%#Eval("CompanyID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="PersonID" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblPID" runat="server" Text='<%#Eval("PersonID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

.CS

 protected void TDetails_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtPID.Text = TDetails.DataKeys[TwicDetails.SelectedRow.RowIndex].Value.ToString();
        txtLastName.Text = (TDetails.SelectedRow.FindControl("lblLastName") as Label).Text;
        txtFirstName.Text = (TDetails.SelectedRow.FindControl("lblFirstName") as Label).Text;
        txtCompany.Text = (TDetails.SelectedRow.FindControl("lblCompany") as Label).Text;
       }

1 个答案:

答案 0 :(得分:1)

您可以修改SQL查询以获取CompanyName以及如何获取CompanyId并将其绑定到gridview并将可见性设置为 false ,最后您可以像设置其他控件一样设置文本框的值: -

SQL查询: -

SELECT CompanyId, CompanyName, ....
FROM tblCompany..

.aspx的: -

 <asp:TemplateField HeaderText="Company" Visible="false">
     <ItemTemplate>
         <asp:Label ID="lblCompanyName" runat="server" Text='<%#Eval("CompanyName") %>' />
     </ItemTemplate>
 </asp:TemplateField>