473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Missing attributes when postback Dropdownlist

LG
I want to add attributes to a custom dropdownlist and I found some code
on Internet that doesn't work after post back.
First time the control has the attributes, but after postback the
attributes are lost.
It must be a bug in the code bellow. Can anyone help?
Thanks,
LG
using System;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Componen tModel;
using System.Collecti ons;

namespace PD.Web.UI.WebCo ntrols
{
/// <summary>
/// Summary description for WebCustomContro l.
/// </summary>
public class PDDropDownList : System.Web.UI.W ebControls.Drop DownList
{

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"Th e HTML writer to write out to </param>

protected override void RenderContents( HtmlTextWriter writer)
{
for(int c=0;c<Items.Cou nt;c++)
{
ListItem i = Items[c];
writer.WriteBeg inTag("option") ;
if(i.Selected)
writer.WriteAtt ribute("selecte d","selected",f alse);
writer.WriteAtt ribute("value", i.Value,true);
IEnumerator d = Items[c].Attributes.Key s.GetEnumerator ();
while(d.MoveNex t())
writer.WriteAtt ribute(d.Curren t.ToString(),It ems[c].Attributes[d.Current.ToStr ing()]);
writer.Write('> ');
System.Web.Http Utility.HtmlEnc ode(i.Text,writ er);
writer.WriteEnd Tag("option");
writer.WriteLin e();
}
}

protected override object SaveViewState()
{
//Saves the ViewState of the dropdownlist
object baseState = base.SaveViewSt ate();
object[] allStates = new object[this.Items.Coun t+1];
allStates[0] = baseState;
for(int c=0;c<Items.Cou nt;c++)
{
int AttrisCount=Ite ms[c].Attributes.Cou nt;
object[] Attris=new object [AttrisCount*2];
int i=0;
IEnumerator d = Items[c].Attributes.Key s.GetEnumerator ();
while(d.MoveNex t())
{
Attris[i++] =d.Current.ToSt ring();
Attris[i++] =Items[c].Attributes[d.Current.ToStr ing()];
}
allStates[c+1] = Attris;
}
return allStates;
// return base.SaveViewSt ate();

}
protected override void LoadViewState(o bject savedState)
{
if (savedState != null)
{
// Load State from the array of objects that was saved at
SavedViewState.
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewSt ate(myState[0]);
for(int c=1;c<myState.L ength;c++)
{
int AttrisCount=((o bject[])myState[c]).Length;
for(int n=0;n<(AttrisCo unt/2);n++)
{
object[] o = (object[])myState[c];
this.Items[c-1].Attributes.Add (o[n].ToString
(),o[n+1].ToString());
}
}
}
// base.LoadViewSt ate(savedState) ;

}

}
}

Oct 2 '06 #1
1 6814
LG,

I am dealing with the same problem at the moment, you are not using ASP
controls you are writing a string to your clients IE. This string is not
rendered by ASPNet and therefore nothing happens, if you want to use the
possibilities of the ASPNET controls (or changed runat server controls) than
you have to do your work using the ASPX page.

TextBox1.Attrib utes("onblur") = "javascript:ale rt('Hello! Focus lost from
text box!!');"

I hope this helps,

Cor
"LG" <lu************ @yahoo.comschre ef in bericht
news:11******** *************@e 3g2000cwe.googl egroups.com...
>I want to add attributes to a custom dropdownlist and I found some code
on Internet that doesn't work after post back.
First time the control has the attributes, but after postback the
attributes are lost.
It must be a bug in the code bellow. Can anyone help?
Thanks,
LG
using System;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Componen tModel;
using System.Collecti ons;

namespace PD.Web.UI.WebCo ntrols
{
/// <summary>
/// Summary description for WebCustomContro l.
/// </summary>
public class PDDropDownList : System.Web.UI.W ebControls.Drop DownList
{

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"Th e HTML writer to write out to </param>

protected override void RenderContents( HtmlTextWriter writer)
{
for(int c=0;c<Items.Cou nt;c++)
{
ListItem i = Items[c];
writer.WriteBeg inTag("option") ;
if(i.Selected)
writer.WriteAtt ribute("selecte d","selected",f alse);
writer.WriteAtt ribute("value", i.Value,true);
IEnumerator d = Items[c].Attributes.Key s.GetEnumerator ();
while(d.MoveNex t())
writer.WriteAtt ribute(d.Curren t.ToString(),It ems[c].Attributes[d.Current.ToStr ing()]);
writer.Write('> ');
System.Web.Http Utility.HtmlEnc ode(i.Text,writ er);
writer.WriteEnd Tag("option");
writer.WriteLin e();
}
}

protected override object SaveViewState()
{
//Saves the ViewState of the dropdownlist
object baseState = base.SaveViewSt ate();
object[] allStates = new object[this.Items.Coun t+1];
allStates[0] = baseState;
for(int c=0;c<Items.Cou nt;c++)
{
int AttrisCount=Ite ms[c].Attributes.Cou nt;
object[] Attris=new object [AttrisCount*2];
int i=0;
IEnumerator d = Items[c].Attributes.Key s.GetEnumerator ();
while(d.MoveNex t())
{
Attris[i++] =d.Current.ToSt ring();
Attris[i++] =Items[c].Attributes[d.Current.ToStr ing()];
}
allStates[c+1] = Attris;
}
return allStates;
// return base.SaveViewSt ate();

}
protected override void LoadViewState(o bject savedState)
{
if (savedState != null)
{
// Load State from the array of objects that was saved at
SavedViewState.
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewSt ate(myState[0]);
for(int c=1;c<myState.L ength;c++)
{
int AttrisCount=((o bject[])myState[c]).Length;
for(int n=0;n<(AttrisCo unt/2);n++)
{
object[] o = (object[])myState[c];
this.Items[c-1].Attributes.Add (o[n].ToString
(),o[n+1].ToString());
}
}
}
// base.LoadViewSt ate(savedState) ;

}

}
}

Oct 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1968
by: Nathan Baulch | last post by:
I have a page with EnableViewState="false". That page contains a DropDownList with dynamic items and a submit Button. I need to get the SelectedIndex of the DropDownList on postback however all I ever get is -1. This makes sense since on postback, the DropDownList is now empty (ViewState data is responsible for maintaining the items). Now...
2
1317
by: Andreas Klemt | last post by:
Hello, for what is the label good when programming in visual studio like this: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Label1: If Page.IsPostBack Then DoThis() End If
0
3515
by: Paul Rees | last post by:
I am wondering if anyone has encountered a similar problem to the one that I am having. I have an ASP.NET page which has a table and a dropdownlist that was added at design time. The majority of the rows in the table are then generated at run-time based on a SQL query from a Jet database, and similarly the dropdownlist is filled at run-time ...
1
1213
by: Florent | last post by:
Hi all I'm developping an ASPX application with C# code behind, with Visual Studio 2003 and .NET 1.1 In my form, I have a DropDownList (filled), and an empty Table (web form), which i want to fill when something is selected in my DropDownList The problem is : when i select something in my DropDownList, I fill the table with whatever i...
4
1624
by: starwiz | last post by:
I'm trying to use the DataGrid's editing with a DropDownList. I've tried using every code sample I've seen and none of them seem to be able to solve my problem. When I call e.Item.FindControl("name-of-my-DropDownList"), where e is a DataGridCommandEventArgs passed to the UpdateCommand of the DataGrid, I get an empty DropDownList. In fact,...
2
2063
by: Gummy | last post by:
Hello All, I have a webpage that has two dropdown listboxes. Based on what is selected in these dropdown listboxes, it filters a DataGrid . That works fine. In the DataGrid , when I go to edit a row, I change the textbox (or other control), click Update, but it doesn't save my data. I then did this... In Page_Load, I added code to...
0
2086
by: Managed Code | last post by:
Hello All, Here is my issue and thanks in advance for any assistance. I have a base page with a dropdownlist that fires an event with the selected index. The content page catches the event and sets a connection string to the database. The content page has a simple gridview that should show records from the selected database. Initial...
2
1827
by: Fabio Mastria | last post by:
Hi all! In a my simple project I use callback to fill a dropdownlist with xml data returned by a web service, based on a value which is input via another dropdownlist. NOTE: I can't use ajax/atlas. Using javascript and callback all works... but if I press a button or any event that raise a postback, the dropdownlist that I fill gets...
2
1400
by: Paul | last post by:
I've written som junk code below to show a problem I'm getting: On a page I've got a dropdownlist and a button using submit behaviour: <asp:DropDownList ID="tester" runat="server" AutoPostBack="true" EnableViewState="False" OnSelectedIndexChanged="tester_SelectedIndexChanged"> <asp:ListItem Text="All" Value="ALL"></asp:ListItem>...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.