enable an asp.net panel on dropdownlist onselectindexchanged - c#

I want to enable a panel based on dropdownlist selected value.
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl.SelectedValue == "A")
{
lnk.Style.Add("Display", "block");
panel1.Visible=true;
panel1.Enabled=true;
}
}
The panel is not getting displayed. I have set autopastback property of dropdownlist to true.Can someone please help me.

should be without the quotes:
panel1.Enabled = true;
but if you wanted to show hide a panel:
<asp:Panel ID="panel1" runat="server" Visible="False" >
...
</asp:Panel>
then the right way would be
panel1.Visible = true;//false to hide

Related

C# Repeater focusing the first element after DataSource is changed

I have a Repeater with a certain DataSource (consisting of a list of images). The Repeater holds ImageButtons.
The aspx:
<asp:Panel ID="panSearch" runat="server" ScrollBars="Vertical" BorderColor="#333333" BorderStyle="Inset" Width="500" Height="200">
<asp:Repeater ID="Repeater" runat="server">
<ItemTemplate>
<asp:ImageButton OnClick="imgSearchResult_Click" BackColor="#333333" ID="imgSearchResult" height="32" width="32" runat="server" ImageUrl='<%# Eval("ImageUrl") %>'/>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
Additionally, I have a TextBox, which has a TextChanged-event in code-behind. I do a few things in there and at the end, my Repeater's DataSource will be overwritten with a new List of images (those images are put into the ImageButtons).
Repeater.DataSource = ImageList;
Repeater.DataBind();
My problem: Whenever my Repeater.DataSource is changed, it "clicks" the first ImageButton inside the Repeater. How do I prevent that from happening?
Full code:
My TextBox:
<asp:TextBox ID="textSearch" runat="server" Width="80" OnTextChanged="textSearch_TextChanged" ForeColor="Black" />
My TextChanged event:
protected void textSearch_TextChanged(object sender, EventArgs e)
{
string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images/ORAS"));
List<System.Web.UI.WebControls.Image> ImageList = new List<System.Web.UI.WebControls.Image>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
System.Web.UI.WebControls.Image myImage= new System.Web.UI.WebControls.Image();
myImage.ImageUrl = (String.Format("~/Images/ORAS/{0}", System.IO.Path.GetFileName(item)));
ImageList.Add(myImage);
}
Repeater.DataSource = ImageList;
Repeater.DataBind();
}
When I click on an ImageButton inside the Repeater (which is executed when the text in my TextBox is changed):
protected void imgSearchResult_Click(object sender, ImageClickEventArgs e)
{
var selectedImage = sender as ImageButton;
if (img1.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img1.ImageUrl = selectedImage.ImageUrl;
}
else if (img2.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img2.ImageUrl = selectedImage.ImageUrl;
}
else if (img3.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img3.ImageUrl = selectedImage.ImageUrl;
}
else if (img4.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img4.ImageUrl = selectedImage.ImageUrl;
}
else if (img5.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img5.ImageUrl = selectedImage.ImageUrl;
}
else if (img6.ImageUrl == "~/Images/ORAS/Empty/000.png")
{
img6.ImageUrl = selectedImage.ImageUrl;
}
else
{
ErrorMessage("Please remove one Image first!", true);
}
}
Pageload:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
img1.ImageUrl = "~/Images/ORAS/Empty/000.png";
img2.ImageUrl = "~/Images/ORAS/Empty/000.png";
img3.ImageUrl = "~/Images/ORAS/Empty/000.png";
img4.ImageUrl = "~/Images/ORAS/Empty/000.png";
img5.ImageUrl = "~/Images/ORAS/Empty/000.png";
img6.ImageUrl = "~/Images/ORAS/Empty/000.png";
LoadImages();
}
}
(LoadImages is almost 1:1 what's in my TextChanged function)
I really am not sure how (why) ASP.NET WebForms does it, but if you hit Enter and the form posts back, it will find the first control that implements IPostBackEventHandler and execute whatever event is bound to that. ImageButton implements it and so that's why it keeps firing the click event even though you didn't click on it. And, once again, only if you hit Enter.
I think that behaviour happens because the data posted back - __EVENTTARGET and __EVENTARGUMENT - are empty. Then ASP.NET goes bonkers.
You can solve it by putting a dummy button at the top of the page (or masterpage) and hide it using the style attribute. so:
<asp:Button ID="dummy" runat="server" style="display:none" />
Then in the init or load of your page (or masterpage) put
Form.DefaultButton = dummy.UniqueID;
That will force the button to capture the enter press instead of the arbitrary image button.

Display different Gridviews with a Dropdownlist onchange

I want to show different Gridviews when I select values from a dropdownlist.I use Visible="False" property to gridviews and I want to show only one on every value updated.
Example: when I select the Value "Points" I want to show GridView1 and when I select Names I want to show "GridView2". This is my ASP:
<asp:DropDownList ID="Stats_Ddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Stats_Ddl_IndexChanged" >
<asp:ListItem>POINTS</asp:ListItem>
<asp:ListItem>NAMES</asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" Visible="False"> blahblahblah1</asp:GridView>
<asp:GridView ID="GridView2" runat="server" Visible="False">blahblahblah12</asp:GridView>
and c# is:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Stats_Ddl_IndexChanged(object sender, EventArgs e)
{
}
Any suggestions for c# code? thanks...
Hi inside event SelectedIndexChanged
you should put for example
If(Ddl.SelectedValue == "1"){
GridView1.Visible = true;
GridView2.Visible = false;
}else{
GridView1.Visible = false;
GridView2.Visible = true;
}
i see that you have a ListItem don't forget give to a value to property "Value" for example
Point = 1
Name = 2
You can use SelectedItem.Text property of DropDownList to branch
protected void Stats_Ddl_IndexChanged(object sender, EventArgs e)
{
if(Stats_Ddl.SelectedItem.Text == "POINTS")
GridView1.Visible = true;
else
if(Stats_Ddl.SelectedItem.Text == "NAMES")
GridView2.Visible = true;
}

DropDown list not firing change event when it is altered

I have a page with a dropdownlist and a button on it. The initial selection on the dropdown is an empty string. I do not want this submitted to the server so I disable the button. I then want to enable the button if any other selection is made on the dropdown. However my ddlBusinessUnit_SelectedIndexChanged method is never hit when I make changes in the dropdown list.
html:
<asp:DropDownList ID="ddlBusinessUnit" EnableViewState="true" runat="server"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged" />
code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsDate.Date = DateTime.Today;
PopulateBusinessUnits();
StatusMessages.Visible = false;
}
bGetFiles.Enabled = false;
}
public void ddlBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlBusinessUnit.SelectedItem.Text != "")
bGetFiles.Enabled = true;
}
Set AutoPostBack="true" for your dropdown.
<asp:DropDownList ID="ddlBusinessUnit" EnableViewState="true" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged" />
you're missing AutoPostBack="true" on your asp dropdown control

Not able to visible button asp.net

I am not able to visible my button on another button click event.
.aspx
<asp:Button ID="btnActivate" runat="server" SkinID="skinLoginButton"
Text="Activate" ToolTip="Activate" CausesValidation="true"
ValidationGroup="UserAuthentication" onclick="btnActivate_Click" />
<asp:Button ID="btnhomepage" Visible="false" runat="server"
Text="Goto Homepage" CssClass="cssLoginButton" onclick="btnhomepage_Click"/>
.cs
#region btnActivate_Click
protected void btnActivate_Click(object sender, EventArgs e)
{
this.btnhomepage.Visible = true;
}
#endregion
I use this.btnhomepage.Visible = true; in .cs file.
what's wrong in my code or declearation?
<asp:Button ID="btnhomepage" Visible="false" runat="server"
Text="Goto Homepage" CssClass="cssLoginButton" onclick="btnhomepage_Click"/>
when using visible attribute in the mark-up you are forcing your control to be visible=false and stay false forever. asp.net engine render asp.net controls into html control in asp.net page life cycle at Render stage. even you had changed the control property in any code behind event
Solution: Don't use makup attribute when setting control behaviour dynamicllay
page life cycle link:
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
http://www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
Remove the visible property from the btnhomepage button and make it invisible from Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.btnhomepage.Visible = false;
}
}
Try this
btnhomepage.Visible = true;
btnhomepage.Enabled = true;
btnhomepage.Style.Add("display", "block");

Dynamic change dropdownlist and textbox by custom control

I want to write a custom control involving a DropDownList and a TextBox.
Actually, I want to dynamically render DropDownList and TextBox.
For example: when a user clicks a Checkbox, the Textbox will change to a DropdownList. On the other hand, when a user deselects the Checkbox, the Dropdownlist will change to a Textbox.
I know this can be done using two controls, which sets the visibility for both control. But can I do it on a custom control?
If you still want to go with that approach, here is your code.
In Design File:-
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox1_CheckedChanged" />
<div id ="control" runat="server">
</div>
In Code Behind File:-
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TextBox txt = new TextBox();
txt.ID = "txt";
control.Controls.Add(txt);
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked)
{
for (int ix = this.Controls.Count - 1; ix >= 0; ix--)
if (this.Controls[ix] is TextBox) this.Controls[ix].Dispose();
DropDownList ddl = new DropDownList();
ddl.ID = "ddl";
control.Controls.Add(ddl);
}
else
{
for (int ix = this.Controls.Count - 1; ix >= 0; ix--)
if (this.Controls[ix] is DropDownList) this.Controls[ix].Dispose();
TextBox txt = new TextBox();
txt.ID = "txt";
control.Controls.Add(txt);
}
}
Hope this is what you were looking for.
You could try this code
ASPX
<%# Control Language="C#" AutoEventWireup="true" CodeFile="dynamicControl.ascx.cs" Inherits="dynamicControl" %>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"
oncheckedchanged="CheckBox1_CheckedChanged" />
<asp:DropDownList ID="DropDownList1" runat="server" visible="false">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
CodeBehind
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
DropDownList1.Visible = CheckBox1.Checked;
TextBox1.Visible = !CheckBox1.Checked;
}
This snippet will show a dropDownList if CheckBox is checked and change to TextBox if it's not checked. Despite this is possible I don't think this is the right approach. (eg: AutoPostBack needed, set visibility...)
What do you try to achieve?

Categories