I'm querying a database filling a gridview with values, also adding dropdown boxes into each cell within the dataview 'onrowdatabound' so these DDL's are populated when the gridview is populated.
我正在查询用数值填充gridview的数据库,还在数据视图'onrowdatabound'中的每个单元格中添加下拉框,以便在填充gridview时填充这些DDL。
I want to be able to click a button to get values from these DDL's however when the button is clicked postback happens and all the DDL's disappear and it gives me the default value for the DDL.
我希望能够单击一个按钮从这些DDL获取值,但是当单击按钮时发生回发并且所有DDL都消失并且它为我提供了DDL的默认值。
I assume they dissapear as they're not called on pageload (which I can't seem to do as they're called onrowdatabound)
我认为他们消失了,因为他们没有被调用页面加载(我似乎无法做,因为他们被称为onrowdatabound)
<asp:GridView id="View" name="Spview" onrowdatabound="populateCellswithDDls" runat="server"></asp:GridView>
Adding the ddl with inside 'populateCellswithDDls' function looping each cell:
在内部'populateCellswithDDls'函数中添加ddl循环每个单元格:
e.Row.Cells[i].Controls.Add(DDL1);
The next thing I've have a play with is ViewState and Sessions to save the dropdownlists on postback(Tried making sessions within 'populateCellswithDDls' function as so:
我接下来要玩的是ViewState和Sessions,以便在回发时保存下拉列表(尝试在'populateCellswithDDls'函数中进行会话,如下所示:
DropDownList DDL1 = new DropDownList();
//I've tried newSkillsMon.AutoPostBack = true; but this just removes them all too
Session.Add("ViewState", View);
Session.Add("DropdownState", DDL1);
I've tried all sorts do to with viewstate and session but unsure where to use them in relation to saving states the 'onrowdatabound' population.
我已经尝试过各种各样的方法来处理viewstate和session但不确定在哪里使用它们来保存'onrowdatabound'种群的状态。
My button currently looks like this:
我的按钮目前看起来像这样:
protected void confirm_Click(object sender, EventArgs e){
{foreach (GridViewRow row in View.Rows)
// if (DDL1.SelectedItem.Value != "Select Item"){
if (IsPostBack)
{
Debug.WriteLine(DDL1.SelectedValue);
}
This just gives me X amount of "Select Item" rather than what i have selected in the DDL
这只是给了我X量的“选择项目”而不是我在DDL中选择的数量
What am I missing, where would I add these sessions to keep the ddl's created by onrowdatabound?
我错过了什么,我将在哪里添加这些会话以保持onrowdatabound创建的ddl?
Thanks
2 个解决方案
#1
0
When creating dynamic controls, you need to recreate them on every Page Load and that includes a PostBack. So start with binding the GridView data outside an IsPostBack
check.
创建动态控件时,需要在每个页面加载上重新创建它们,并包含PostBack。因此,首先在IsPostBack检查之外绑定GridView数据。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//normally you would bind here
}
//but now bind grid every page load
GridView1.DataSource = Common.LoadFromDB();
GridView1.DataBind();
}
Now in the RowDataBound event make sure the DropDownList has an ID
现在在RowDataBound事件中确保DropDownList具有ID
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//create a dropdownlist and assign an ID
DropDownList DDL1 = new DropDownList();
DDL1.ID = "DDL1";
//add some dummy listitems
DDL1.Items.Insert(0, new ListItem() { Text = "A", Value = "A" });
DDL1.Items.Insert(1, new ListItem() { Text = "B", Value = "B" });
DDL1.Items.Insert(2, new ListItem() { Text = "C", Value = "C" });
//add the control to the row
e.Row.Cells[0].Controls.Add(DDL1);
}
}
Now you can get the value from the correct row on a button click.
现在,您可以通过单击按钮从正确的行中获取值。
protected void Button1_Click(object sender, EventArgs e)
{
//find the dropdownlist in the correct row and cast it back to one
DropDownList DDL1 = GridView1.Rows[i].FindControl("DDL1") as DropDownList;
//display the result
Label1.Text = DDL1.SelectedValue;
}
#2
0
Something that might work for you instead of trying sessions is to use hiddenfields. Make a hidden field for each dropdown location and then use javascript to populate the hidden fields with the dropdown values. (Possibly wanting some server side validation on submission)
可能对你有用而不是尝试会话的东西是使用hiddenfields。为每个下拉列表位置创建一个隐藏字段,然后使用javascript用下拉值填充隐藏字段。 (可能希望在提交时进行一些服务器端验证)
Something like this with jQuery:
用jQuery这样的东西:
$(".dropdowns").on("change", function () {
$(this).closest('input:hidden').val($(this).val());
});
And in your confirmation:
在您的确认中:
if (HF_HiddenField1.Value != "Select Item")
#1
0
When creating dynamic controls, you need to recreate them on every Page Load and that includes a PostBack. So start with binding the GridView data outside an IsPostBack
check.
创建动态控件时,需要在每个页面加载上重新创建它们,并包含PostBack。因此,首先在IsPostBack检查之外绑定GridView数据。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//normally you would bind here
}
//but now bind grid every page load
GridView1.DataSource = Common.LoadFromDB();
GridView1.DataBind();
}
Now in the RowDataBound event make sure the DropDownList has an ID
现在在RowDataBound事件中确保DropDownList具有ID
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//create a dropdownlist and assign an ID
DropDownList DDL1 = new DropDownList();
DDL1.ID = "DDL1";
//add some dummy listitems
DDL1.Items.Insert(0, new ListItem() { Text = "A", Value = "A" });
DDL1.Items.Insert(1, new ListItem() { Text = "B", Value = "B" });
DDL1.Items.Insert(2, new ListItem() { Text = "C", Value = "C" });
//add the control to the row
e.Row.Cells[0].Controls.Add(DDL1);
}
}
Now you can get the value from the correct row on a button click.
现在,您可以通过单击按钮从正确的行中获取值。
protected void Button1_Click(object sender, EventArgs e)
{
//find the dropdownlist in the correct row and cast it back to one
DropDownList DDL1 = GridView1.Rows[i].FindControl("DDL1") as DropDownList;
//display the result
Label1.Text = DDL1.SelectedValue;
}
#2
0
Something that might work for you instead of trying sessions is to use hiddenfields. Make a hidden field for each dropdown location and then use javascript to populate the hidden fields with the dropdown values. (Possibly wanting some server side validation on submission)
可能对你有用而不是尝试会话的东西是使用hiddenfields。为每个下拉列表位置创建一个隐藏字段,然后使用javascript用下拉值填充隐藏字段。 (可能希望在提交时进行一些服务器端验证)
Something like this with jQuery:
用jQuery这样的东西:
$(".dropdowns").on("change", function () {
$(this).closest('input:hidden').val($(this).val());
});
And in your confirmation:
在您的确认中:
if (HF_HiddenField1.Value != "Select Item")