2

今天在处理我客户的一个项目时,我遇到了一个我无法找出解决方案的问题。所以我把它扔给你们。

我正在使用数据绑定的 GridView 控件,SqlDataSource 的代码相同:

<asp:SqlDataSource ID="SqlDataSource_familyMembers" runat="server" 
     ConnectionString="<%$ ConnectionStrings:VBAT_dbConnectionString %>" 
     SelectCommand="
       select 
          a.memberid,a.fristName,a.lastname,a.gender,a.dob,
          b.relation as [Relation with Family Head],
      c.districtname,d.statename,e.countryname,a.mobno,f.occupation,a.ismaried 
      from 
         family_members_info as a
      left outer join
         master_relations as b
      on
         a.RelationWithFamilyHead=b.relationid
      join
         master_district as c
      on 
         a.cadistrict=c.districtid
      join
         master_state as d
      on 
        a.castate=d.stateid
     join
        master_country as e
     on 
        a.cacountry=e.countryid
     join
       master_occupation as f
     on
       a.occupation=f.occupationid
     where 
         familyid=@familyid" 
   UpdateCommand="
         update 
             family_members_info 
         set                    
            firstName=@firstname, lastname=@lastName, dob=@dob,
            relationwithfamilyhead=@relationwithfamilyhead,cadistrict=@cadistrict, 
            castate=@castate, cacountry=@cacountry,mobno=@mobno,occupation=@occupation 
         where 
            memberID=@memberID">
  <SelectParameters>                    
    <asp:ControlParameter ControlID="HiddenField1" Name="familyid"   PropertyName="Value" />
  </SelectParameters>
  <UpdateParameters>
      <asp:Parameter Name="firstname"  />
      <asp:Parameter Name="lastName" />
      <asp:Parameter Name="dob" />
      <asp:Parameter Name="relationwithfamilyhead" />
      <asp:Parameter Name="cadistrict" />
      <asp:Parameter Name="castate" />
      <asp:Parameter Name="cacountry" />
      <asp:Parameter Name="mobno" />
      <asp:Parameter Name="occupation" />
      <asp:Parameter Name="memberID" />
   </UpdateParameters>
</asp:SqlDataSource>

正如您从 Select 查询中看到的那样,数据来自多个相关的表,除了用户想要编辑数据时,一切正常。

对于所有列,默认情况下,GridView 显示文本框,而它应该为作为外键的列显示 DropdownList。

如果有人可以指导我为此提供可能的解决方案,我将非常有帮助。

4

1 回答 1

1

To achieve what you're looking for follow these high level steps:

  1. Template your grid view - Create TemplateField items for each column in your grid view, for both ItemTemplate and EditItemTemplate
  2. For id fields specify the control type for EditItemTemplate to be a DropDownList
  3. Create a SqlDataSource or any other data binding method of your choice and bind it to the DropDownList to retrieve the ids and value from the required table
  4. Set the DataValueField of the DropDownList to be the id and DataTextField to be the display value

Here's a working example of the points above - Filtering Dropdownlist populated from sqldatasource

于 2013-02-25T07:13:20.657 回答