|
can you post the markup of your page and we will point out the problem. Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
Hi
I have a stored procedure which returns first name, lastname and id of an employee
Now this result has become a datasource for my dropdownlist
what i want to do is in the textfield, i want to display in the below format
firstname lastname [id]. I want to do this in front end only not in procedure. Kindly help me how to do this?
Regards
Naina
Naina
|
|
|
|
|
There are many ways of doing so.
1. You can populate your drop down something list like this:
foreach(DataRow row in myDataTable.Rows)
{
string text = row["FirstName"].ToString() + " " + row["LastName"].ToString() + " [" + row["Id"].ToString() + "]";
DropDownList1.Items.Add(new ListItem(text, row["Id"].ToString()));
}
2. You can use linq projection on the dataTable and use teh linq query result as your DropDownList datasource.
Hope this will help.
|
|
|
|
|
Thanks for your reply.... I got it.
Naina
|
|
|
|
|
Hi
I am having one more prob.... when selecting the item from dropdown , i want only the firstname from ddl.SelectedItem.Text
Naina
Naina
|
|
|
|
|
You can get firstname by just splitting ddl.SelectedItem.Text
|
|
|
|
|
Instead of adding the ID to the listItem value, add the first name as below:
foreach(DataRow row in myDataTable.Rows)
{
string text = row["FirstName"].ToString() + " " + row["LastName"].ToString() + " [" + row["Id"].ToString() + "]";
DropDownList1.Items.Add(new ListItem(text, row["FirstName"].ToString()));
}
and then retrieve this, rather than the text using:
ddl.SelectedItem.Value
|
|
|
|
|
This is better approch
|
|
|
|
|
Hi
I have a link button which is part of my user control. Further, the control is actually added to the control dynamically. Whilst, I can see the control, and is added to the page without any visible errors, but when I click on to fire an event which on that user control nothing happens. What is even more odd, if I add a break point to event, and then click in debug mode, I can't even reach the break point!
To try and resolve this, I have tried giving the link button ids, and ensured they unique, added override using OnInit and used the createchildcontrols method, but nothing seems to work!
Has anyone came across and if so, can give me a solution?
|
|
|
|
|
Where in the code is the linkbutton created dynamically? Put a breakpoint on page_load and you should see that this is hit when you click the linkbutton. It doesn't fire the event because the control is not recreated again as part of the postback.
Try recreating the control in page_load if isPostBack = true. Remember to recreate your event handler here as well.
|
|
|
|
|
Hi
I had already done all of this.
The linkbutton is created on the usercontrol form.
I have added the linkbutton creation to, with this.linkbutton1.click += EventHandler(this.linkbutton1_click); to Page_Load, OnInit, Page_PreRender and CreateChildControls.
The event will not fire even after doing all of this!
|
|
|
|
|
Could you post the code behind please?
|
|
|
|
|
As you said the Link button is created Dynamically, you have to create it Every Time when the page is post back, also create it in the Init event of the user control.
|
|
|
|
|
Here is the user control with the link button on it!
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
protected override void CreateChildControls( )
{
base.CreateChildControls( );
this.lnkShowAllCust.ID = "lnkShowAllCust";
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
this.Controls.Add(lnkShowAllCust);
}
protected override void OnInit (EventArgs e)
{
CreateChildControls( );
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.EnsureChildControls( );
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
CreateChildControls( );
}
}
protected void lnkShowAllCust_Click(object sender, EventArgs e)
{
this.OnCustShowAllClicked(new EventArgs ( ));
}
protected virtual void OnCustShowAllClicked(EventArgs args)
{
if (this.ViewAllClicked != null)
{
this.ViewAllClicked(this, args);
}
}
public event EventHandler ViewAllClicked;
}
I have deleted other stuff that is on it, but these other controls have nothing to do with the link button
|
|
|
|
|
Here is the user control with the link button on it!
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
protected override void CreateChildControls( )
{
base.CreateChildControls( );
this.lnkShowAllCust.ID = "lnkShowAllCust";
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
this.Controls.Add(lnkShowAllCust);
}
protected override void OnInit (EventArgs e)
{
CreateChildControls( );
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.EnsureChildControls( );
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
CreateChildControls( );
}
}
protected void lnkShowAllCust_Click(object sender, EventArgs e)
{
this.OnCustShowAllClicked(new EventArgs ( ));
}
protected virtual void OnCustShowAllClicked(EventArgs args)
{
if (this.ViewAllClicked != null)
{
this.ViewAllClicked(this, args);
}
}
public event EventHandler ViewAllClicked;
}
I have deleted other stuff that is on it, but these other controls have nothing to do with the link button
|
|
|
|
|
Try following code:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
LinkButton lnk = new LinkButton();
lnk.Text = "My Link";
lnk.Click += new EventHandler(lnk_Click);
plcHld.Controls.Add(lnk);
}
void lnk_Click(object sender, EventArgs e)
{
lblMessage.Text = "Link Clicked";
}
|
|
|
|
|
I have tried this, and still no luck.
One thing I did omit to mention is that I have an AJAX control on the ASPX page that encompasses the whole user control. All this does is to refresh the page every 5 mins.
I commented this AJAX control out, added a break point to the link button event, and all that happened was the java script fired, but it didn't reach the break point.
|
|
|
|
|
This is a simple example of recreating your control on postback. So assuming that works and you still have the problem: is there any validation on the front end? Could it be that the form isn't valid so you don't get the postback?
public partial class WebUserControl1 : System.Web.UI.UserControl
{
static int x = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (ViewState["x"] != null)
{
CreateLB();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateLB();
}
private void CreateLB()
{
LinkButton lb = new LinkButton();
lb.Text = "click me";
lb.Click += new EventHandler(lb_Click);
this.Controls.Add(lb);
ViewState["x"] = true;
}
void lb_Click(object sender, EventArgs e)
{
x += 1;
Label1.Text = x.ToString();
}
}
front end:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
|
|
|
|
|
Hi
There is no form validation at all on the page.
The only other thing I have is an AJAX Control, which refreshes the page every 5 mins.
I will try your code, and see how I get on!
|
|
|
|
|
Hi
I have tried your code, and nothing happened.
However, as part of my debugging and trying to get to grips with this, I added a break point to the page_load method and ran again.
The page_load method didn't get hit, I even then moved the code from there to OnLoad, and it did get hit, but inside the if statement for the viewstate, it skipped over the CreateLB() method!
Any thoughts?
|
|
|
|
|
Have you wired up the button on the front page to hit right method:
protected void Button1_Click(object sender, EventArgs e)
{
CreateLB();
}
The idea is, you click the button on the form, this dynamically creates the linkbutton, then when you click the linkbutton it stays there and is recreated on postback every time you click it.
|
|
|
|
|
Yes, I have.
Since my reply to you, I have been digging a bit deeper, and my feelings at the moment are starting to sway if the button event is not being wired up correctly. Therefore, I double checked the AutoWireEvent, and this has been set to true on the usercontrol.
By the way, the Page_Load is being overriden by the OnLoad method, which I need to have on my page, because I am setting up a grid view, which needs to reload on PostBack, because it is also built dynamically.
What I have also done is put on a simple button on the control, and ran the ocde in debug through OnLoad. This has set the button text value, and I can see the event handler being added in. Next I clicked the button on Post Back. When the page is reload via OnLoad, which also has a if (IsPostBack) this is hit, and what I wanted to do is to reset the text value to something else.
However, what happened instead was that the button disappeared off the screen. Event for that button was never reached!
Am I missing a trick here?
|
|
|
|
|
how to simulate on demand combo in asp.net. i want when user click on arrow button of combo then combo will load the data. how to do this.Thanks in advance
tbhattacharjee
|
|
|
|
|
Why Dont you go for autocomplete extender..!
or you can go to teletrix controls thts only a suggestion
LatestArticle :Log4Net
Why Do Some People Forget To Mark as Answer .If It Helps.
|
|
|
|
|
Well, using auto-complete extender of ajax in textbox
should not be a bad idea to achieve what you want.
If you stil wish to populate combo box, call a WebMethod
on click of your combo box (drop down list actually), then get the
data in xml format, read that xml using javascript and then attach the
items to your combo box.
|
|
|
|