I have DetailsView that automatically selects data from a datasource. Within that detailsview I have 2 dropdown lists bound from a different datasource. On update I want the text and value of each drop down list to store into the database.

I can't seem to get the value of the dropdown lists so I can update the paramaters before the SQL command is executed. Or if there is another way to do so that'd be great.

<asp:DetailsView ID="UserProfile" runat="server"
     AutoGenerateRows="False" DataKeyNames="UserId" DefaultMode="Edit"
     DataSourceID="UserProfileDataSource" onitemupdated="UserProfile_ItemUpdated" OnPageIndexChanging="UserProfile_PageIndexChanging">
     <Fields>
          <asp:BoundField DataField="fname" HeaderText="First Name:"
               SortExpression="fname" />
         <asp:BoundField DataField="lname" HeaderText="Last Name:"
               SortExpression="lname" />
         <asp:BoundField DataField="address" HeaderText="Address:"
               SortExpression="address" />
         <asp:BoundField DataField="city" HeaderText="City:"
               SortExpression="city" />
          <asp:TemplateField HeaderText="State:" SortExpression="state">
              <EditItemTemplate>
                  <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="State_en" DataValueField="StateId">
                  </asp:DropDownList>
              </EditItemTemplate>
              <InsertItemTemplate>
                  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("state") %>'></asp:TextBox>
              </InsertItemTemplate>
              <ItemTemplate>
                  <asp:Label ID="Label1" runat="server" Text='<%# Bind("state") %>'></asp:Label>
              </ItemTemplate>
          </asp:TemplateField>
         <asp:BoundField DataField="zip" HeaderText="Zip Code:"
               SortExpression="zip" />
         <asp:BoundField DataField="phoneNum" HeaderText="Phone Number:"
               SortExpression="phoneNum" />
          <asp:TemplateField HeaderText="Auto Carrier:" SortExpression="autoCarrier">
              <EditItemTemplate>
                  <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="CarrerID">
                  </asp:DropDownList>
              </EditItemTemplate>
              <InsertItemTemplate>
                  <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("autoCarrier") %>'></asp:TextBox>
              </InsertItemTemplate>
              <ItemTemplate>
                  <asp:Label ID="Label2" runat="server" Text='<%# Bind("autoCarrier") %>'></asp:Label>
              </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="policyNum" HeaderText="Policy Number:"
               SortExpression="policyNum" />
                     <asp:CommandField ShowEditButton="True" />
     </Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="UserProfileDataSource" runat="server"
          ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
          SelectCommand="SELECT [fname], [lname], [address], [city], [state], [zip], [phoneNum], [autoCarrier], [policyNum], [accountid] ,[UserId] FROM [UserProfile] WHERE ([UserId] = @UserId)" 
    OnSelecting="UserProfileDataSource_Selecting" 
    OnUpdating="OnSqlUpdating" 
    UpdateCommand="UPDATE UserProfile SET
    stateId = @stateId,
    carrierId = @carrierId,
    fname = @fname,
    lname= @lname,
    address = @address,
    city = @city,
    state = @state,
    zip = @zip,
    phoneNum = @phoneNum,
    autoCarrier = @autoCarrier,
    policyNum = @policyNum
    WHERE ([UserId] = @UserId)">
     <SelectParameters>
          <asp:Parameter Name="UserId" Type="Object" />
     </SelectParameters>
     <UpdateParameters>
         <asp:Parameter Name="fname" />
         <asp:Parameter Name="lname" />
         <asp:Parameter Name="address" />
         <asp:Parameter Name="city" />
         <asp:Parameter Name="state" />
         <asp:Parameter Name="zip" />
         <asp:Parameter Name="phoneNum" />
         <asp:Parameter Name="autoCarrier" />
         <asp:Parameter Name="policyNum" />
         <asp:Parameter Name="stateId" />
         <asp:Parameter Name="carrierId" />
         <asp:Parameter Name="UserId" />
     </UpdateParameters>
</asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [StateId], [State_en] FROM [States]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
        SelectCommand="SELECT [CarrerID], [Name] FROM ### WHERE ([AccountID] = (SELECT [accountid] FROM ### WHERE ([UserId] = @UserId)))" OnSelecting="UserProfileDataSource_Selecting">
    <SelectParameters>
          <asp:Parameter Name="UserId" Type="Object" />
     </SelectParameters>
    </asp:SqlDataSource>

Here's the code Behind that I wrote, but doesn't work:

protected void OnSqlUpdating(object sender, SqlDataSourceCommandEventArgs e)
{

    string state = DropDownList1.SelectedItem.Text;
    string autoCarrier = DropDownList2.SelectedItem.Text;
    string stateId = DropDownList1.SelectedItem.Value;
    string carrierId = DropDownList2.SelectedItem.Value;

    e.Command.Parameters["@state"].Value = state;
    e.Command.Parameters["@autoCarrier"].Value = autoCarrier;
    e.Command.Parameters["@stateId"].Value = stateId;
    e.Command.Parameters["@carrierId"].Value = carrierId;
}

I keep getting:

Error 99 The name 'DropDownList1' does not exist in the current context Error 102 The name 'DropDownList1' does not exist in the current context
Error 101 The name 'DropDownList2' does not exist in the current context
Error 103 The name 'DropDownList2' does not exist in the current context

I'm new to ASP so any suggestions you recommend or cleaning up of bad code practices is appreciated.

有帮助吗?

解决方案

you can not access DropDownList1 and DropDownList2 directly because there are TemplateField for the DetailsView control. After data binding you can use :

DropDownList1 myDropDownList =(DropDownList)UserProfile..Rows[0].Cells[0].FindControl("DropDownList1");
string state = myDropDownList .SelectedItem.Text;

for passing the parameter you need to use OnUpdating Event.

<asp:SqlDataSource ID="UserProfileDataSource" runat="server"  

OnUpdating="UserProfileDataSource_Updating"

          ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
          SelectCommand="SELECT [fname], [lname], [address], [city], [state], [zip], [phoneNum], [autoCarrier], [policyNum], [accountid] ,[UserId] FROM [UserProfile] WHERE ([UserId] = @UserId)" 
    OnSelecting="UserProfileDataSource_Selecting" 
    OnUpdating="OnSqlUpdating" 
    UpdateCommand="UPDATE UserProfile SET
    stateId = @stateId,
    carrierId = @carrierId,
    fname = @fname,
    lname= @lname,
    address = @address,
    city = @city,
    state = @state,
    zip = @zip,
    phoneNum = @phoneNum,
    autoCarrier = @autoCarrier,
    policyNum = @policyNum
    WHERE ([UserId] = @UserId)">
     <SelectParameters>
          <asp:Parameter Name="UserId" Type="Object" />
     </SelectParameters>
     <UpdateParameters>
         <asp:Parameter Name="fname" />
         <asp:Parameter Name="lname" />
         <asp:Parameter Name="address" />
         <asp:Parameter Name="city" />
         <asp:Parameter Name="state" />
         <asp:Parameter Name="zip" />
         <asp:Parameter Name="phoneNum" />
         <asp:Parameter Name="autoCarrier" />
         <asp:Parameter Name="policyNum" />
         <asp:Parameter Name="stateId" />
         <asp:Parameter Name="carrierId" />
         <asp:Parameter Name="UserId" />
     </UpdateParameters>
</asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [StateId], [State_en] FROM [States]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
        SelectCommand="SELECT [CarrerID], [Name] FROM ### WHERE ([AccountID] = (SELECT [accountid] FROM ### WHERE ([UserId] = @UserId)))" OnSelecting="UserProfileDataSource_Selecting">
    <SelectParameters>
          <asp:Parameter Name="UserId" Type="Object" />
     </SelectParameters>
    </asp:SqlDataSource>

then in the code behind

  protected void UserProfileDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
            {
                e.Command.Parameters["@ParamaterName"].Value = // The Value you get from the drop         downlist
            }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top