简体   繁体   English

在插入新条目时使用 GridView 中的 DropDownList 的值

[英]Using the value of a DropDownList in a GridView on inserting a new entry

I searched for a few days now but didn't find a solution.我现在搜索了几天,但没有找到解决方案。 So here is my scenario:所以这是我的场景:

I have a DetailView with a DataBindung to a database and DefaultMode set to " Insert " as a input-form for a new db-entry.我有一个带有 DataBindung 的 DetailView 到数据库和 DefaultMode 设置为“插入”作为新数据库条目的输入表单。 One field in this DetailsView is a TemplateField with a InsertItemTemplate containing a DropDownList.此 DetailsView 中的一个字段是具有包含 DropDownList 的 InsertItemTemplate 的 TemplateField。 This DropDownList is also bounded to another SqlDataSource, using a select-parameter "UserID" to get all rows of a database for the actual logged-in user.此 DropDownList 还绑定到另一个 SqlDataSource,使用选择参数“UserID”为实际登录用户获取数据库的所有行。 Those rows are shown in my DropDownList and when looking at the sourcecode in my browser I see the correct "option"-attributes with all those nice db-IDs in their corresponding value.这些行显示在我的 DropDownList 中,在我的浏览器中查看源代码时,我看到了正确的“选项”-属性,所有这些漂亮的 db-ID 都在其相应的值中。 So everything is fine to this point.所以到目前为止一切都很好。

Now I press my Insert-Button to send my data back to the server.现在我按下我的插入按钮将我的数据发送回服务器。 But when trying to get the selected value from my DropDownList in DetailsView1_ItemInserting() with the following code I get a null-value.但是,当尝试使用以下代码从我的 DropDownList 中的 DetailsView1_ItemInserting() 中获取选定的值时,我得到了一个空值。

var list = (DropDownList)DetailsView1.Rows[0].FindControl( "DropDownList1" );
InsertEntryDataSource.InsertParameters["DropDownList1Value"].DefaultValue = list.Items[list.SelectedIndex].Value;

The same code works with another form I created in this project.相同的代码适用于我在这个项目中创建的另一个表单。 The only difference is, that the other DropDownList is bounded to a DataSource without a select-parameter I have to set on runtime (the current logged-in UserID).唯一的区别是,另一个 DropDownList 绑定到一个数据源,没有我必须在运行时设置的选择参数(当前登录的用户 ID)。

My resumption was that the DropDownList is empty after the postback because the databinding wasn't yet done.我的结论是,回发后 DropDownList 为空,因为数据绑定尚未完成。 So I set the select-parameter for the SqlDataSource my DropDownList is bounded to in DetailsView1_ItemInserting() - and now the above line got my value.所以我在 DetailsView1_ItemInserting() 中为我的 DropDownList 绑定到的 SqlDataSource 设置了选择参数 - 现在上面的行得到了我的值。

BUT : I just tested the form another time and I determined, that no matter which entry I select in my DropDownList, I get always the value of the first entry!但是:我刚刚又测试了一次表单,我确定,无论我在 DropDownList 中选择哪个条目,我总是得到第一个条目的值! The whole procedure seems to clear my SelectedIndex of the DropDownList - so now I have a value to insert instead of null, but it's the wrong one.整个过程似乎清除了 DropDownList 的 SelectedIndex - 所以现在我有一个值要插入而不是 null,但它是错误的。 :-( :-(

So my question: How can I access the selected value of my DropDownList in DetailsView1_ItemInserting() to save it to the database?所以我的问题是:如何在 DetailsView1_ItemInserting() 中访问我的 DropDownList 的选定值以将其保存到数据库中?

Thanks in advance, Anheledir提前致谢, Anheledir

You can use this syntax to access the ddl without writing code:您可以使用此语法访问 ddl,而无需编写代码:

<InsertParameters>
<asp:ControlParameter ControlID="DetailsView1$DropDownList1" Name="DropDownList1" PropertyName="SelectedValue" Type="String" />
</InsertParameters>

or from the DataSource_Inserting event and not the DetailsView_ItemInserting event u can do this:或者从 DataSource_Inserting 事件而不是 DetailsView_ItemInserting 事件你可以这样做:

  e.InputParameters["InputParameterFieldName"] = ((DropDownList)DetailsView1.FindControl("DropDownList1")).SelectedValue;

Good Luck!祝你好运!

Are you ever inserting more than one row ata a time?您是否曾经一次插入不止一行数据? I'd like to back up a step and ask why it is you need to have the insert as part of the GridView itself and not it's own part of the UI (with some input controls and a submit button).我想备份一个步骤并询问为什么您需要将插入作为 GridView 本身的一部分而不是它自己的 UI 部分(带有一些输入控件和提交按钮)。 The separation in space emphasizes the separation of function - one part for inserting, one part for reporting/display.空间的分离强调功能的分离——一部分用于插入,一部分用于报告/展示。 The method you're using sounds like a lot of struggle for little reward.您使用的方法听起来像是为了获得很少的回报而进行的大量斗争。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM