Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

unpredictable behavior of asp.net controls in webparts

  Asked By: Varsha    Date: Sep 17    Category: Sharepoint    Views: 1369

i initially added one TextBox and a Button in a webpart
programmatically and on Button_Click event i captured the text of
TextBox , this task was done successfully.

Then i added a ListBox and a Button, now on button_Click i am trying
to capture selected item in ListBox through ListBox.SelectedValue or
ListBox.SelectedItem.Value none of it is working. i tried almost
everything but unable to capture selected value on postback when
button is clicked.

I repeated the listbox process by replacing ListBox with DropDownList
but had no effect.

So if any one come up with solution do let me know

Share: 

 

4 Answers Found

 
Answer #1    Answered By: David Scott     Answered On: Sep 17

It is possible that you are trying to access the listbox item  before the
postback has been processed properly.

 
Answer #2    Answered By: Lakeshia Gould     Answered On: Sep 17

this.DropDownList1.AutoPostBack = true; //very important

DropDownList1.SelectedIndexChanged +=new
EventHandler(DropDownList1_SelectedIndexChanged);

Controls.Add(DropDownList1);

 
Answer #3    Answered By: Lizbeth Macdonald     Answered On: Sep 17

its still not working  properly i am not able to retrive dropdownlist
or list box selected  value even on button_click nor it selected index
changed event  is calling properly u can check my provide code below

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;


namespace AreaManager
{
/// <summary>
/// Description for WebPart1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:AreaManager runat=server></
{0}:AreaManager>"),
XmlRoot(Namespace="AreaManager")]
public class AreaManager :
Microsoft.SharePoint.WebPartPages.WebPart
{
private button  btnAdd = null;
private Label lblMessage = null;
private TextBox txtArea = null;
private DropDownList lstAreas = null;
private Button btnDelete = null;


private Microsoft.SharePoint.WebControls.FormDigest
FormDigest1;
private AreaService.AreaService areaService = null;
private ListItem lstDataAreas = null;
private bool check = false;



protected override void CreateChildControls()
{
//base.CreateChildControls ();
try
{

//Text Box For Area Title Input
txtArea = new TextBox();
txtArea.Text = "";
txtArea.Visible = true;
Controls.Add(txtArea);

//Add button for creating area
btnAdd = new Button();
btnAdd.Text = "Create Area";
btnAdd.Visible = true;
btnAdd.Click += new EventHandler
(btnAdd_Click);
Controls.Add(btnAdd);

//Message for temporary messages
lblMessage = new Label();
lblMessage.Text = "";
lblMessage.Visible = true;
Controls.Add(lblMessage);

//Area List Box thats lists down
areas under home hierarchy
lstAreas = new DropDownList();
lstAreas.EnableViewState = true;
lstAreas.AutoPostBack = true;
lstAreas.Visible = true;
lstAreas.SelectedIndexChanged += new
EventHandler(lstAreas_SelectedIndexChanged);
Controls.Add(lstAreas);

btnDelete = new Button();
btnDelete.Text = "Delete Area";
btnDelete.Visible = true;
btnDelete.Click += new EventHandler
(btnDelete_Click);
Controls.Add(btnDelete);

}
catch(Exception ex)
{
lblMessage.Text =
ex.Message+" .. "+ex.StackTrace;
}
}


/// <summary>
/// Render this Web Part to the output parameter
specified.
/// </summary>
/// <param name="output"> The HTML writer to write
out to </param>
protected override void RenderWebPart(HtmlTextWriter
output)
{
//output.Write(SPEncode.HtmlEncode(Text));
try
{

this.Page.EnableViewState =
true;
txtArea.RenderControl(output);
btnAdd.RenderControl(output);
output.WriteLine("<br>");
lblMessage.RenderControl
(output);
output.WriteLine("<br>");
populateList();
lstAreas.RenderControl
(output);
output.WriteLine("<br>");
btnDelete.RenderControl
(output);
output.WriteLine("<br>");

}
catch(Exception ex)
{
lblMessage.Text =
ex.Message+" .. "+ex.StackTrace;
}
}


private void populateList()
{
try
{
areaService = new
AreaService.AreaService();
areaService.Credentials =
System.Net.CredentialCache.DefaultCredentials;
Guid homeGuid =
areaService.GetHomeAreaID();
Guid searchGuid =
areaService.GetSearchAreaID();
Guid []subareas =
areaService.GetSubAreas(homeGuid);
for(int i = 0; i<subareas.Length;++i)
{
if(subareas[i] != searchGuid)
{
lstDataAreas = new
ListItem(areaService.GetAreaData(subareas
[i]).AreaName,areaService.GetAreaData(subareas[i]).AreaID.ToString());
lstAreas.Items.Add
(lstDataAreas);
}
}
lstAreas.EnableViewState = true;
}
catch(Exception ex)
{
lblMessage.Text =
ex.Message+" .. "+ex.StackTrace;
}
}



private void lstAreas_SelectedIndexChanged(object
sender, System.EventArgs e)
{
try
{

lblMessage.Text = "Firing Event";
lblMessage.Text +=
lstAreas.SelectedIndex.ToString();

}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
}
}

private void btnAdd_Click(object sender,
System.EventArgs e)
{
try
{
lblMessage.Text = "Firing Add event";
areaService = new
AreaService.AreaService();
areaService.Credentials =
System.Net.CredentialCache.DefaultCredentials;
string strTitle = String.Empty;
if(txtArea.Text == "")
{
strTitle = "testArea2";
}
else
strTitle = txtArea.Text;

Guid homeGuid =
areaService.GetHomeAreaID();
Guid newAreaGuid =
areaService.CreateArea(homeGuid,strTitle,"SPSNHOME");
AreaService.AreaData areaData =
areaService.GetAreaData(newAreaGuid);
lblMessage.Text = "Newly Created Area
Name is "+areaData.AreaName+" created
on "+areaData.CreationDate.ToShortDateString();
if( newAreaGuid!=
areaService.GetSearchAreaID())
{
lstDataAreas = new ListItem
(areaService.GetAreaData(newAreaGuid).AreaName,areaService.GetAreaData
(newAreaGuid).AreaID.ToString());
//ddList.Items.Add
(lstDataAreas);
lstAreas.Items.Add
(lstDataAreas);
}
txtArea.Text = "";


}
catch(Exception ex)
{
lblMessage.Text =
ex.Message+" .. "+ex.StackTrace;

}
}


/*public void btnMoveRight_Click(object sender,
System.EventArgs e)
{
try
{
lstDelAreas.Items.Add(new ListItem
(lstAreas.SelectedItem.Text,lstAreas.SelectedItem.Value));

}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
}

}*/

public void btnDelete_Click(object sender,
System.EventArgs e)
{
try
{
lblMessage.Text = "Firing Delete
Event";
lblMessage.Text +=
lstAreas.SelectedIndex.ToString();
/* areaService = new
AreaService.AreaService();
lblMessage.Text = "Success 1";
areaService.Credentials =
System.Net.CredentialCache.DefaultCredentials;
lblMessage.Text += "Success 2";
//string areaGuid =
lstAreas.SelectedItem.Value;
lblMessage.Text += "Success 3";
bool flag = false;
lblMessage.Text += "Success 4";
lblMessage.Text += lstAreas.Items
[lstAreas.SelectedIndex].Value;
string areaGuid = lstAreas.Items
[lstAreas.SelectedIndex].Value;;
//Guid guid = new Guid(areaGuid);

for(int j = 0; j<
lstAreas.Items.Count;++j)
{
if(flag == false)
{
lblMessage.Text
+= "Success 6";
if(areaGuid ==
lstAreas.Items[j].Value )
{

lblMessage.Text += "Success 7";

areaService.DeleteArea( new Guid(lstAreas.Items[j].Value) );

lblMessage.Text += "Success 8";

lstAreas.Items.Remove(new ListItem(lstAreas.Items
[j].Text,lstAreas.Items[j].Value));

lblMessage.Text += "Success 9";
flag = true;

lblMessage.Text += "Success 10";
}
}

}
lblMessage.Text += "Success 11";
*/
}
catch(Exception ex)
{
lblMessage.Text += ex.Message;
}
}

}
}

 
Answer #4    Answered By: Cole Curtis     Answered On: Sep 17

.interesting...i looked at your code...i'm doing exactly what you are
doing..but i see you are not calling EnsureChildControls();
So try this
protected override void RenderWebPart(HtmlTextWriter output)
{
EnsureChildControls();// First line
....rest of your code...
}

 
Didn't find what you were looking for? Find more on unpredictable behavior of asp.net controls in webparts Or get search suggestion and latest updates.




Tagged: