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

时间:2022-09-25 23:55:28

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. One field in this DetailsView is a TemplateField with a InsertItemTemplate containing a DropDownList. 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. 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. So everything is fine to this point.

我有一个带有DataBindung的DetailView到数据库,DefaultMode设置为“Insert”作为新db-entry的输入形式。此DetailsView中的一个字段是TemplateField,其InsertItemTemplate包含DropDownList。此DropDownList也与另一个SqlDataSource绑定,使用select-parameter“UserID”获取实际登录用户的数据库的所有行。这些行显示在我的DropDownList中,当我在浏览器中查看源代码时,我看到了正确的“选项” - 所有那些不错的db-ID在其对应的值中的属性。所以到目前为止一切都很好。

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.

现在我按下我的插入按钮将我的数据发送回服务器。但是当尝试使用以下代码从DetailsView1_ItemInserting()中的DropDownList中获取所选值时,我得到一个空值。

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被绑定到DataSource而没有我必须在运行时设置的select-parameter(当前登录的UserID)。

My resumption was that the DropDownList is empty after the postback because the databinding wasn't yet done. 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.

我的恢复是在回发后DropDownList为空,因为数据绑定尚未完成。所以我设置了SqlDataSource的select-parameter我的DropDownList在DetailsView1_ItemInserting()中被绑定 - 现在上面的行得到了我的值。

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! 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中选择哪个条目,我总是得到第一个条目的值!整个过程似乎清除了我的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

2 个解决方案

#1


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!

#2


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). 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.

你曾经一次插入多行吗?我想备份一个步骤并询问为什么你需要将插入作为GridView本身的一部分而不是它自己的UI部分(带有一些输入控件和一个提交按钮)。空间的分离强调功能的分离 - 一部分用于插入,一部分用于报告/显示。你正在使用的方法听起来像很少的奖励。

#1


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!

#2


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). 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.

你曾经一次插入多行吗?我想备份一个步骤并询问为什么你需要将插入作为GridView本身的一部分而不是它自己的UI部分(带有一些输入控件和一个提交按钮)。空间的分离强调功能的分离 - 一部分用于插入,一部分用于报告/显示。你正在使用的方法听起来像很少的奖励。