Hi,
I've been trying to add two dropdown lists and a button to a web part. I want to pass the values a user selects in the dropdowns to a filter on that web part.
However, when the values are submitted (either with a button click on a SelectedIndexChanged event, the page posts, but the values are lost.)
In an effort to test, I'm just tyring to write the passed values to the page, but they always return null. If I use an IsPostback event in the CreateChildControls method, the dropdownlists lose all of their values (though from what I've read, I thought they should keep them)?
I'm sure I'm missing something on how ViewState works with SharePoint. Any help is greatly appreciated. (Code snippet below.)
Thanks!
Sami
public void CreateDDLsAndButtons()
{
try
{
ddlOption1.ID = "ddlOption1";
ddlOption2.ID = "ddlOption2";
ListItem liTitle = new ListItem();
liTitle.Value = "Title";
liTitle.Text = "Title";
ddlOption1.Items.Add(liTit
le);
ddlOption2.Items.Add(liTit
le);
ListItem liStudent = new ListItem();
liStudent.Value = "Author";
liStudent.Text = "Student";
ddlOption1.Items.Add(liStu
dent);
ddlOption2.Items.Add(liStu
dent);
//If I disable the AutoPostBack, the SelectedIndexChanged doesn't get called, though I'm not sure it's getting called when this is enabled, although the page does post back.
ddlOption1.AutoPostBack = true;
ddlOption2.AutoPostBack = true;
ddlOption1.SelectedIndexCh
anged += new System.EventHandler(this.d
dlOption1_
SelectedIn
dexChanged
);
ddlOption2.SelectedIndexCh
anged += new System.EventHandler(this.d
dlOption2_
SelectedIn
dexChanged
);
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Go";
btnSubmit.Click += new System.EventHandler(this.b
tnSubmit_C
lick);
}
catch (Exception ex)
{
WriteToEventLog(ex.ToStrin
g(), "Multigenre Research", EventLogEntryType.Error, "Application");
}
}
protected override void CreateChildControls()
{
base.CreateChildControls()
;
CreateDDLsAndButtons();
this.Controls.Add(ddlOptio
n1);
this.Controls.Add(ddlOptio
n2);
this.Controls.Add(btnSubmi
t);
FunctiontoUseTheValues(sOp
tion1, sOption2);
}
private void ddlOption1_SelectedIndexCh
anged(obje
ct sender, System.EventArgs e)
{
//I've tried putting this in a session var as well and no luck.
sOption1 = ddlOption1.SelectedItem.Va
lue;
HttpContext.Current.Respon
se.Write("
<br />1: " + HttpContext.Current.Sessio
n["sOption
1"]);
}
private void ddlOption2_SelectedIndexCh
anged(obje
ct sender, System.EventArgs e)
{
sOption2 = ddlOption2.SelectedItem.Va
lue;
HttpContext.Current.Respon
se.Write("
<br />2: " + HttpContext.Current.Sessio
n["sOption
2"]);
}
private void btnSubmit_Click(object sender, System.EventArgs e)
{
sOption1 = ddlOption1.SelectedItem.Va
lue;
HttpContext.Current.Respon
se.Write("
<br />Option 1 value: " + sOption1);
}
Start Free Trial