我在试着做一份定制的申请表。到目前为止,我已经添加了一些控件,比如TextBox和Button,一切都很好,直到我用一个DropDownList控件修改了一些TextBox。当我单击add按钮时,它返回一个错误:
'categoryDropDownList‘有一个无效的SelectedValue,因为它不存在于项目列表中。参数名称:值
我已将代码的示例代码附在后面:
public partial class Test2 : System.Web.UI.Page
{
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["LibrarySystemConnectionString"].ConnectionString;
}
private void execution(string bookid, string booktitle, string lastname, string firstname, string description, string categoryid, string dateadded, string quantity, string statusid)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO TblBooks(bookid, booktitle, lastname, firstname, description, categoryid, dateadded, quantity, statusid) VALUES "+" (@bookid, @booktitle,@lastname, @firstname, @description, @categoryid, @dateadded, @quantity, @statusid)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] pram = new SqlParameter[9];
pram[0] = new SqlParameter("@bookid", SqlDbType.BigInt, 64);
pram[1] = new SqlParameter("@booktitle", SqlDbType.NVarChar, 50);
pram[2] = new SqlParameter("@lastname", SqlDbType.NVarChar, 50);
pram[3] = new SqlParameter("@firstname", SqlDbType.NVarChar, 50);
pram[4] = new SqlParameter("@description", SqlDbType.NVarChar, 200);
pram[5] = new SqlParameter("@categoryid", SqlDbType.Int, 32);
pram[6] = new SqlParameter("@quantity", SqlDbType.Int, 32);
pram[7] = new SqlParameter("@statusid", SqlDbType.Int, 32);
pram[8] = new SqlParameter("@dateadded", SqlDbType.DateTime, 32);
pram[0].Value = bookid;
pram[1].Value = booktitle;
pram[2].Value = lastname;
pram[3].Value = firstname;
pram[4].Value = description;
pram[5].Value = categoryid;
pram[6].Value = quantity;
pram[7].Value = statusid;
pram[8].Value = dateadded;
for (int i = 0; i < pram.Length; i++)
{
cmd.Parameters.Add(pram[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex_msg)
{
string msg = "Error occured while inserting";
msg += ex_msg.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (bookidTextBox.Text == "")
{
Response.Write("Please complete the form");
}
else
{
execution(bookidTextBox.Text, booktitleTextBox.Text, lastnameTextBox.Text, firstnameTextBox.Text, descriptionTextBox.Text, categoryDropDownList.SelectedValue, dateaddedTextBox.Text, quantityTextBox.Text, statusDropDownList.SelectedValue);
//conform.Visible = true;
bookidTextBox.Text = "";
booktitleTextBox.Text = "";
lastnameTextBox.Text = "";
firstnameTextBox.Text = "";
descriptionTextBox.Text = "";
categoryDropDownList.SelectedValue = "";
dateaddedTextBox.Text = "";
statusDropDownList.SelectedValue = "";
quantityTextBox.Text = "";
}
}
}
}下面是表单的示例代码:
<table class="style1">
<tr>
<td class="style2">
ISBN:
</td>
<td class="style3">
<asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>'>
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ControlToValidate="bookidTextBox" ErrorMessage="* " ValidationGroup="addbooks">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
Title:
</td>
<td class="style3">
<asp:TextBox ID="booktitleTextBox" runat="server" Text='<%# Bind("booktitle") %>'>
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="booktitleTextBox" ErrorMessage="* " ValidationGroup="addbooks">
</asp:RequiredFieldValidator>
</td>
</tr>
... (cut some code here)
<tr>
<td class="style2">
Category:
</td>
<td class="style3">
<asp:DropDownList ID="categoryDropDownList" runat="server"
DataSourceID="categoryDataSource" DataTextField="name"
DataValueField="categoryid" >
</asp:DropDownList>
<asp:SqlDataSource ID="categoryDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]">
</asp:SqlDataSource>
</td>
</tr>
...
<tr>
<td class="style2">
Status:</td>
<td class="style3">
<asp:DropDownList ID="statusDropDownList" runat="server"
DataSourceID="statusDataSource" DataTextField="statusname"
DataValueField="statusid" >
</asp:DropDownList>
<asp:SqlDataSource ID="statusDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [statusid], [statusname] FROM [BookStatus]">
</asp:SqlDataSource>
</td>
</tr>
</table>
</div>
<asp:Button ID="AddBtn" runat="server" Text="Add" OnClick="Button1_Click" Width="100px" />
<br />
<br />
<b>Book List</b>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="bookid" DataSourceID="bookDataSource">
<Columns>
<asp:BoundField DataField="bookid" HeaderText="bookid" ReadOnly="True"
SortExpression="bookid" />
<asp:BoundField DataField="booktitle" HeaderText="booktitle"
SortExpression="booktitle" />
<asp:BoundField DataField="lastname" HeaderText="lastname"
SortExpression="lastname" />
<asp:BoundField DataField="firstname" HeaderText="firstname"
SortExpression="firstname" />
<asp:BoundField DataField="categoryid" HeaderText="categoryid"
SortExpression="categoryid" />
<asp:BoundField DataField="description" HeaderText="description"
SortExpression="description" />
<asp:BoundField DataField="dateadded" HeaderText="dateadded"
SortExpression="dateadded" />
<asp:BoundField DataField="statusid" HeaderText="statusid"
SortExpression="statusid" />
<asp:BoundField DataField="quantity" HeaderText="quantity"
SortExpression="quantity" />
<asp:CheckBoxField DataField="isdeleted" HeaderText="isdeleted"
SortExpression="isdeleted" />
</Columns>
</asp:GridView>当我试图将DropDownList转换为textbox时,它成功了,但是如何将它与DropDownList控件绑定呢?
我会非常感谢你的帮助!提前谢谢。
发布于 2011-06-04 00:55:49
您的DropDown绑定到SqlDataSource。当您执行categoryDropDownList.SelectedValue = ""时,DropDown在数据源中寻找空值,但是找不到,所以失败了。
如果你想要运行dropDownList.ClearSelection(),如果这是你想要实现的。
下面是演示问题的简化代码示例
protected void Page_Load(object sender, EventArgs e)
{
var items = new[] { new { Id = 1, Name = "Test1" }, new { Id = 2, Name = "Test2" } };
dropDownList.DataSource = items;
dropDownList.DataValueField = "Id";
dropDownList.DataTextField = "Name";
dropDownList.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
dropDownList.SelectedValue = ""; // trhows exception
dropDownList.ClearSelection(); // works
}发布于 2011-06-04 00:59:35
读一读这篇文章:
http://msdn.microsoft.com/en-us/library/system.web.ui.page.enableeventvalidation.aspx
https://stackoverflow.com/questions/6233942
复制相似问题