我有DetailsView自动从数据源中选择数据。在该详细信息视图中,我有2个从不同数据源绑定的下拉列表。在更新时,我希望每个下拉列表的文本和值都存储到数据库中。
我似乎无法获得下拉列表的值,因此我可以在执行SQL命令之前更新参数。或者,如果还有其他方法可以做到这一点,那就太好了。
<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>
这是我写的代码背后,但不起作用:
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;
}
我一直在:
错误99当前上下文中不存在名称“DropDownList1” 错误102当前上下文中不存在名称“DropDownList1” 错误101当前上下文中不存在名称“DropDownList2” 错误103当前上下文中不存在名称“DropDownList2”
我是ASP的新手,所以您推荐的任何建议或清理不良代码的做法都会受到赞赏。
答案 0 :(得分:2)
您无法直接访问DropDownList1
和DropDownList2
,因为TemplateField
控件有DetailsView
。数据绑定后,您可以使用:
DropDownList1 myDropDownList =(DropDownList)UserProfile..Rows[0].Cells[0].FindControl("DropDownList1");
string state = myDropDownList .SelectedItem.Text;
用于传递您需要使用OnUpdating事件的参数。
<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>
然后在
背后的代码中 protected void UserProfileDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["@ParamaterName"].Value = // The Value you get from the drop downlist
}