0
我想限制使用DropDownList INSERT的選擇,我的TextBox都正常工作,但我似乎無法弄清楚如何以類似的方式使用DropDownList。如何使用DropDownList限制用戶在數據庫INSERT中的選擇?
這裏是我當前的代碼隱藏:
private void _subInsert(RepeaterCommandEventArgs e)
{
TextBox txtPostTitle = (TextBox)e.Item.FindControl("txt_post_titleI"); // Find the control with the information you wish to store
TextBox txtPostBody = (TextBox)e.Item.FindControl("txt_post_bodyI");
TextBox txtPostAuthor = (TextBox)e.Item.FindControl("txt_post_authorI");
DropDownList ddlPostType = (DropDownList)e.Item.FindControl("ddl_post_typeI");
sds_main.InsertParameters["post_title"].DefaultValue = txtPostTitle.Text; // Insert information into this location in the DB
sds_main.InsertParameters["post_body"].DefaultValue = txtPostBody.Text;
sds_main.InsertParameters["post_author"].DefaultValue = txtPostAuthor.Text;
sds_main.InsertParameters["post_type"].DefaultValue = ddlPostType.SelectedValue.ToString();
sds_main.Insert(); // Perform Insert
}
文本框都正確插入,但將DropDownList不允許我使用的SelectedValue的插入。我得到一個「未設置爲對象實例的對象引用」。錯誤。
這是演示頁面上我SQLDataSource控件的代碼:
<asp:SqlDataSource
id="sds_main"
runat="server"
ConnectionString="<%$ ConnectionStrings:fntn0070DBConnectionString %>"
SelectCommand="SELECT * FROM [blogTest]"
DeleteCommand="DELETE FROM [blogTest] WHERE [id] = @id"
InsertCommand="INSERT INTO [blogTest] ([post_title], [post_body], [post_author], [post_type]) VALUES (@post_title, @post_body, @post_author, @post_type)"
UpdateCommand="UPDATE [blogTest] SET [post_title] = @post_title, [post_body] = @post_body, [post_author] = @post_author, [post_type] = @post_type WHERE [id] = @id" >
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="post_title" Type="String" />
<asp:Parameter Name="post_body" Type="String" />
<asp:Parameter Name="post_date" Type="String" />
<asp:Parameter Name="post_author" Type="String" />
<asp:Parameter Name="post_type" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="post_title" Type="String" />
<asp:Parameter Name="post_body" Type="String" />
<asp:Parameter Name="post_author" Type="String" />
<asp:Parameter Name="post_type" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
這是我的展示頁面上DDL:
<asp:DropDownList ID="ddl_postTypeI" runat="server">
<asp:ListItem>Web Development</asp:ListItem>
<asp:ListItem>Photography</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
我已經盡了全力搜索此問題在Google和StackOverflow上,但只能找到關於如何將數據綁定到DDL的信息。
我對ASP.NET 4很新,剛剛開始在我的網站開發課程中學習它,所以如果能夠回答,請致電ELI5。預先感謝您提供的任何幫助。
哇,沒有意識到修復是多麼簡單。非常感謝你。 – Neil
評價答案如果您認爲這對您有幫助... – Hitesh