<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" Width="809px"
DataSourceID="ObjectDataSource3" EnableModelValidation="True">
<InsertItemTemplate> <table class="tab2" border="1" cellpadding="0" cellspacing="0" frame="border">
<tr>
<td class="style1">
<asp:Label ID="Label1" runat="server" Text="产品标题:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox1" runat="server" CssClass="textbox1" Text='<%#Bind("proname") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label2" runat="server" Text="产品类别:"></asp:Label>
</td>
<td class="style2">
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1" AutoPostBack="true" DataTextField="categoryname" DataValueField='<%#Bind("categoryid") %>' > </asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="getcategory" TypeName="category" ></asp:ObjectDataSource>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label3" runat="server" Text="产品品牌:"></asp:Label>
</td>
<td class="style2">
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="ObjectDataSource2" DataTextField="brandname" DataValueField='<%#Bind("brandid") %>'>
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
SelectMethod="getbrand" TypeName="brand"></asp:ObjectDataSource>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label4" runat="server" Text="产品概述:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox2" runat="server" CssClass="textbox_multi"
TextMode="MultiLine" Text='<%#Bind("prosummary") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label5" runat="server" Text="产品图片:"></asp:Label>
</td>
<td class="style2">
<asp:FileUpload ID="FileUpload1" runat="server" CssClass="textbox1" />
</td>
</tr>
<tr>
<td class="style1">
</td>
<td class="style2">
<asp:Label ID="Label6" runat="server" Text="上传图片的地址"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" CssClass="button1" Text="添加" CommandName="Insert" />
<asp:Button ID="Button2" runat="server" CausesValidation="False"
CssClass="button1" Text="重置" />
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
</table> </InsertItemTemplate>
</asp:FormView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource3" runat="server" TypeName="pro" InsertMethod="insertpro" >
<InsertParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="categoryid" Type="Int32"/>
<asp:ControlParameter ControlID="TextBox1" Name="proname" />
<asp:ControlParameter ControlID="TextBox2" Name="prosummary" />
<asp:ControlParameter ControlID="DropDownList2" Name="brandid" Type="Int32"/>
</InsertParameters>
</asp:ObjectDataSource> 上面是.aspx页面,下面是添加的类----------------- public void insertpro(string proname,int categoryid,int brandid,string prosummary)
{
SqlConnection con = new SqlConnection(_connstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert pro (proname,categoryid,brandid,prosummary) values (@proname,@categoryid,@brandid,@prosummary)";
cmd.Parameters.AddWithValue("@proname",proname);
cmd.Parameters.AddWithValue("@categoryid",categoryid);
cmd.Parameters.AddWithValue("@brandid", brandid);
cmd.Parameters.AddWithValue("@prosummary", prosummary); using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
}提示错误为: Could not find control 'DropDownList1' in ControlParameter 'categoryid'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Could not find control 'DropDownList1' in ControlParameter 'categoryid'.在线等候,谢谢各位
DataSourceID="ObjectDataSource3" EnableModelValidation="True">
<InsertItemTemplate> <table class="tab2" border="1" cellpadding="0" cellspacing="0" frame="border">
<tr>
<td class="style1">
<asp:Label ID="Label1" runat="server" Text="产品标题:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox1" runat="server" CssClass="textbox1" Text='<%#Bind("proname") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label2" runat="server" Text="产品类别:"></asp:Label>
</td>
<td class="style2">
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1" AutoPostBack="true" DataTextField="categoryname" DataValueField='<%#Bind("categoryid") %>' > </asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="getcategory" TypeName="category" ></asp:ObjectDataSource>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label3" runat="server" Text="产品品牌:"></asp:Label>
</td>
<td class="style2">
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="ObjectDataSource2" DataTextField="brandname" DataValueField='<%#Bind("brandid") %>'>
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
SelectMethod="getbrand" TypeName="brand"></asp:ObjectDataSource>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label4" runat="server" Text="产品概述:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="TextBox2" runat="server" CssClass="textbox_multi"
TextMode="MultiLine" Text='<%#Bind("prosummary") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label5" runat="server" Text="产品图片:"></asp:Label>
</td>
<td class="style2">
<asp:FileUpload ID="FileUpload1" runat="server" CssClass="textbox1" />
</td>
</tr>
<tr>
<td class="style1">
</td>
<td class="style2">
<asp:Label ID="Label6" runat="server" Text="上传图片的地址"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" CssClass="button1" Text="添加" CommandName="Insert" />
<asp:Button ID="Button2" runat="server" CausesValidation="False"
CssClass="button1" Text="重置" />
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
</table> </InsertItemTemplate>
</asp:FormView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource3" runat="server" TypeName="pro" InsertMethod="insertpro" >
<InsertParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="categoryid" Type="Int32"/>
<asp:ControlParameter ControlID="TextBox1" Name="proname" />
<asp:ControlParameter ControlID="TextBox2" Name="prosummary" />
<asp:ControlParameter ControlID="DropDownList2" Name="brandid" Type="Int32"/>
</InsertParameters>
</asp:ObjectDataSource> 上面是.aspx页面,下面是添加的类----------------- public void insertpro(string proname,int categoryid,int brandid,string prosummary)
{
SqlConnection con = new SqlConnection(_connstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert pro (proname,categoryid,brandid,prosummary) values (@proname,@categoryid,@brandid,@prosummary)";
cmd.Parameters.AddWithValue("@proname",proname);
cmd.Parameters.AddWithValue("@categoryid",categoryid);
cmd.Parameters.AddWithValue("@brandid", brandid);
cmd.Parameters.AddWithValue("@prosummary", prosummary); using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
}提示错误为: Could not find control 'DropDownList1' in ControlParameter 'categoryid'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Could not find control 'DropDownList1' in ControlParameter 'categoryid'.在线等候,谢谢各位
<asp:ControlParameter ControlID="DropDownList1" Name="categoryid" Type="Int32"/>
改为<asp:ControlParameter Name="hours" ControlID="DropDownList1" PropertyName="SelectedValue" Type="Int32" />
前面绑的时候用SelectedValue来绑定
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
DataSourceID="ObjectDataSource1" AutoPostBack="true" DataTextField="categoryname" DataValueField='<%#Bind("categoryid") %>' >这个写法有问题吧
看这个DEMO
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="Emp ID #" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
<asp:TemplateField HeaderText="Hours Worked" SortExpression="hours">
<EditItemTemplate>
<asp:DropDownList ID="someDDL" runat="server" SelectedValue='<%# Bind("hours") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelHours" runat="server" Text='<%# Bind("hours") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:timesheetConnectionString2 %>"
SelectCommand="SELECT * FROM [test_table]"
UpdateCommand="Update [test_table] set [address] = @address, [name] = @name, [hours] = @hours">
<UpdateParameters>
<asp:Parameter Name="name" />
<asp:ControlParameter Name="hours" ControlID="someDDL" PropertyName="SelectedValue" Type="decimal" />
<asp:Parameter Name="address" />
<asp:Parameter Name="id"/>
</UpdateParameters>
</asp:SqlDataSource>
AutoPostBack="True" DataTextField'<%#Bind("categoryname") %>'DataValueField='<%#Bind("categoryid") %>' > </asp:DropDownList>
如果我同事需要 dropdownlist 的 datafieldtext 值和 datafieldvalue 值,你那个写法怎么用呢?这里我正是要用两个数据都要用,取得categoryID同时取得categoryname
dropdownlist.SelectedValue 实际值 categoryID
dropdownlist.SelectedText 显示值 categoryname
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1" AutoPostBack="true" DataTextField="categoryname" DataValueField="categoryid"> </asp:DropDownList>
然后关联:
<asp:ControlParameter Name="hours" ControlID="DropDownList1" PropertyName=”SelectedValue” />
<asp:ControlParameter Name="hours" ControlID="DropDownList1" PropertyName=”SelectedText” />