Dropdown in webpart lost its state after postback

A

Alex

Dear colleagues, I am trying to develop Sharepoint 2k3 webPart for
currency calculator. Since I want to achieve the
visibility/invisibility effect of certain rows, I realized that the
HTML table which consists of those rows should better be generated
during CreateChildControls() instead of traditional
RenderWebPart(HtmlTextWriter output) method.
The problem is that after postback, dropdown list losts its content,
while it was initially populated during Load phase with Dataset
delivered by appropriate web service. As opposed, textbox preserves its
state.
There must be some issue about initializing control tree causing the
viewstate to be lost.

Any assistance would be highly appreciated. Many regards, Alex


Here's the code:


namespace wpCalc1
{
[DefaultProperty("Text"),
ToolboxData("<{0}:cwpCalc1 runat=server></{0}:cwpCalc1>"),
XmlRoot(Namespace="wpCalc1")]
public class cwpCalc1 : Microsoft.SharePoint.WebPartPages.WebPart
{
private Table tbl1 = null;
private TextBox txt1 = null;
...
private DropDownList dd1 = null;
...
private Button btn1 = null;

protected override void CreateChildControls()
{
txt1 = new TextBox();
...
this.Controls.Add(txt1);
this.Controls.Add(txt10);
...
dd1 = new DropDownList();
dd1.Load += new EventHandler(this.dajPodatke1);
this.Controls.Add(dd1);
....
btn1 = new Button();
btn1.Click += new EventHandler(this.dajKonverziju);
this.Controls.Add(btn1);

tbl1 = new Table();

TableRow tr0 = new TableRow();
TableCell c00 = new TableCell();
tr0.Cells.Add(c00);
TableCell c01 = new TableCell();
c01.Controls.Add(this.txt1);
tr0.Cells.Add(c01);
...
TableCell c03 = new TableCell();
c03.Controls.Add(this.dd1); //
tr0.Cells.Add(c03);
...
tbl1.Rows.Add(tr0);
...
TableRow tr10 = new TableRow();
TableCell c100 = new TableCell();
tr10.Cells.Add(c100);
...
TableCell c105 = new TableCell();
c105.Controls.Add(this.btn1);
tr10.Cells.Add(c105);
tbl1.Rows.Add(tr10);

tbl1.Load += new EventHandler(this.dajTabelu);
this.Controls.Add(tbl1);
}

protected override void RenderWebPart(HtmlTextWriter output)
{
EnsureChildControls();
this.tbl1.RenderControl(output);
}
protected void dajTabelu(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
tbl1.Rows[1].Visible = false;
...
tbl1.Rows[9].Visible = false;
}
}
protected void dajPodatke1(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
wpCalc1.sys_report_01.svcCalc ws = new
wpCalc1.sys_report_01.svcCalc();
ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
DataSet ds1 = ws.ws_vratiNoviKurs();

dd1.DataSource = ds1;
dd1.DataValueField = "tfKurs";
dd1.DataTextField = "tfValuta";
dd1.DataBind();
}
}
protected void dajKonverziju(object sender, EventArgs e)
{
string[] kursevi1 = dd1.SelectedValue.ToString().Split(new Char[] {
';' });
decimal EfKup1 = decimal.Parse(kursevi1[0]);
decimal EfPro1 = decimal.Parse(kursevi1[1]);
...

if(dd1.SelectedItem.Text == dd2.SelectedItem.Text)
{
tbl1.Rows[1].Visible = false;
...
tbl1.Rows[9].Visible = false;
}
...
if((dd1.SelectedItem.Text == "RSD") && !(dd1.SelectedItem.Text ==
dd2.SelectedItem.Text))
{
...
if (EfPro2 != 0)
txt3.Text =
Convert.ToString(Math.Round(Math.Abs((Convert.ToDecimal(txt1.Text) *
Convert.ToDecimal(EfPro1) / Convert.ToDecimal(EfPro2))), 3));
...
}
public override ToolPart[] GetToolParts()
{
ToolPart[] toolparts = new ToolPart[2];
WebPartToolPart wptp = new WebPartToolPart();
toolparts[0] = wptp;
toolparts[1] = new CopyrightToolpart();
return toolparts;
}
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top