DropDownButton conditional statement with attributes? - asp.net

I'm having trouble to find out how to check if my DropDownList has the attribute Disabled
Here is my code (of how I declare my DropDownList):
<div class="col-7">
<asp:DropDownList ID="cmbProperty" runat="server" class="browser-default z-depth-5">
</asp:DropDownList>
</div>
On page load:
protected void Page_Load(object sender, EventArgs e)
{
cmbProperty.Attributes.Add("disabled", "disabled");
}
On button click:
protected void btnCheckMyProperty_Click(object sender, EventArgs e)
{
if(cmbProperty.Enabled == true)
{
// I always get a true statement
}
}
Someone has a clue about it?
Thank you

Since you commented saying setting cmbProperty.Enabled = false messes with your css, you should check the disabled attribute in your button click event instead of the Enabled property. This is simply:
protected void btnCheckMyProperty_Click(object sender, EventArgs e)
{
if(cmbProperty.Attributes["disabled"] == "disabled")
{
// Your code here...
}
}
Note: This will NOT error out if the disabled attribute is not set. It will return false in that case...

Related

ASP.NET Setting Visible = false doesn't stay

I have a lot of buttons that I want to appear when I tell them to and so far I have been using Visible = true or false depending on when I want them to appear or not. I want the button to disappear after the click event has occurred on it. I have two buttons in the code and when I click on btnOption131 it disappears but than when I click on btnOption132 it disappears and btnOption131 re-appears. Is there anyway I can stop this from happening?
<asp:Button ID="btnOption131" runat="server" Text="Think" OnClick="btnOption131_Click" Visible="False" CssClass="button"/>
<asp:Button ID="btnOption132" runat="server" Text="Go Crazy" OnClick="btnOption132_Click" Visible="False"/>
Code Behind:
protected void btnOption131_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[8].ToString();
btnOption131.EnableViewState = false;
btnOption131.Visible = false;
}
protected void btnOption132_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[9].ToString();
btnOption132.EnableViewState = false;
btnOption132.Visible = false;
}
I also tried:
protected void btnOption131_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[8].ToString();
btnOption131.Visible = false;
}
protected void btnOption132_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[9].ToString();
btnOption132.Visible = false;
}
You have to set the button Visibility to 'true' somewhere. My guess is that it is not wrapped inside an IsPostBack check:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
btnOption131.Visible = true;
}
}
If you do not do this the visibility will always be set to true when the next PostBack (= button click) occurs.

I can't seem to get css or programmatically changing imagebuttons image

this is a two part problem, one is with CSS and the other with codebehind..
Here is my navigation code for my buttons...
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Buttons/upviewassets.png" OnClick="ImageButton1_Click" />
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Buttons/upaddassets.png" OnClick="ImageButton2_Click" />
All the buttons are side by side. On the OnClick() event my code is this...
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton1.ImageUrl = "Buttons/dnviewassets.png";
ImageButton2.ImageUrl = "Buttons/upaddassets.png";
//Response.Redirect("~/WebForm1.aspx");
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
ImageButton1.ImageUrl = "Buttons/upviewassets.png";
ImageButton2.ImageUrl = "Buttons/dnaddassets.png";
//Response.Redirect("~/WebForm1.aspx");
}
When i comment out the response.redirect it works, but i need to be able to use the response.redirect because this is in a masterpage and i need these buttons to redirect to other pages, when I run it with the response.redirect method the images don't change. As well, when i first run it without the response.redirect, when i click on the button it jumps quickly and goes back to where it should be, but then works fine everytime after.
So for the second part, I have also tried using css to change the imagebuttons image but couldn't get it to work, and i have searched online and went through tutorials but even when using the code provided it didn't work properly and my buttons kept jumping.
I'm trying to mimic a look of tabs in the master page.
Thanks
I would save a flag in the session in click event and retrieve it in redirected page PreRender:
protected override void OnPreRender(EventArgs e)
{
if (!IsPostBack)
{
PaintButtons();
}
base.OnPreRender(e);
}
And my PaintButtons method:
private void PaintButtons()
{
if(Session["ImageButton_Toggled"] == null )
{
ImageButton1.ImageUrl = "Buttons/upviewassets.png";
ImageButton2.ImageUrl = "Buttons/upaddassets.png";
}
else
{
int toggleId = 1;
int.TryParse(Session["ImageButton_Toggled"].ToString(), out toggleId);
if (toggleId == 1)
{
ImageButton1.ImageUrl = "Buttons/dnviewassets.png";
ImageButton2.ImageUrl = "Buttons/upaddassets.png";
}
else
{
ImageButton1.ImageUrl = "Buttons/upviewassets.png";
ImageButton2.ImageUrl = "Buttons/dnaddassets.png";
}
}
}
Click event methods in my masterpage:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Session["ImageButton_Toggled"] = 1;
Response.Redirect("~/WebForm1.aspx");
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
Session["ImageButton_Toggled"] = 2;
Response.Redirect("~/WebForm1.aspx");
}
Now I can redirect to any page I want, and my masterpage will work as expected.

Dropdownlist in gridview not firing selectedindex changed event

I have problem with not firing selected index changed event of dropdownlist in gridview. I gone through the SO Thread . It is not worked wholly for me. I have implementation like below.
.ASPX
<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDL1_SelectedIndexChanged">
<asp:ListItem Text="Review" Value="Review" Selected="True">Review</asp:ListItem>
<asp:ListItem Text="Level1" Value="lvl1">Send Back to Level1</asp:ListItem>
</asp:DropDownList>
.CS
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
}
protected void grdPoll_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(Page.IsPostBack)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DDL1") as DropDownList;
if(ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(DDL1_SelectedIndexChanged);
}
}
}
}
When i keep if(!Page.IsPostBack) block only then it works fine. But i want else block also. Whats going wrong with implentation. Can you please suggest the solutions
The problem is block after !Page.IsPostBack block, which is not event else part as you said. You are binding grid again on post back which results in loss of the event being fired. You do not have to bind it again to have the changes in the grid.
Remove this code.
{
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
//DataBindGrid(); //remove DataBindGrid(); from else
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
DataBindGrid();
}
replace event name "Page_Load" with "Page_PreRender"

Add ServerClick event to ASP.NET <button> element

I am looking to handle the click event of an HTML <button> (not an <input type="button">) on the back end. The reason I want to handle a button is because the design I have has button controls all over the place, so to modify all of the CSS would be much more work.
For my button, I have:
<button type="submit" id="submit" runat="server">Send</button>
In my code behind, I have:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
submit.ServerClick += new EventHandler(submit_click);
}
}
protected void submit_click(object sender, EventArgs e)
{
// never being hit
}
Is this possible?
You should remove the conditional check for if (Page.IsPostBack). Your code will not currently work because the onclick handler only gets set when the page gets first rendered. Then when the user clicks on the button a postback occurs but now your page code no longer has the handler associated with the button so there is no code to execute. Just do this:
protected void Page_Load(object sender, EventArgs e)
{
submit.ServerClick += new EventHandler(submit_click);
}
protected void submit_click(object sender, EventArgs e)
{
// now it should get hit
}
Another way is to just add it to the button element:
<button type="submit" id="submit" runat="server" onserverclick="Button1_OnClick">Send</button>
void Button1_OnClick(object Source, EventArgs e)
{
// Do Something here
}
http://msdn.microsoft.com/en-us/library/a8fd2268%28v=VS.100%29.aspx

Am I supposed to be able to alter other controls from an ASP.NET page event handler?

I'm trying to disable a label control from the CheckedChanged event handler of a checkbox. Should I be able to do this?
At the moment if I set Enabled to false nothing changes when the page reloads. If I do the same thing in Page_Load then I see the change.
To clarify:
This doesn't work:
protected void chkNeverExpires_CheckedChanged(object sender, EventArgs e)
{
this.lblMessage.Enabled = false
}
But this does:
protected void Page_Load(object sender, System.EventArgs e)
{
this.lblMessage.Enabled = false
}
Are you sure you're events are firing in the order you expect? Put breakpoints on all your postback methods and watch what happens, are you resetting the enabled state anywhere? do you have enableviewstate=false on anything?
Edit: You realise CheckedChanged doesnt fire until you postback from another control, or you have AutoPostBack=true on the checbkbox?
This works fine:
<asp:Label runat="server" ID="lblTest">test</asp:Label>
<asp:CheckBox runat="server" ID="chkCheck" AutoPostBack="true" />Check
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
chkCheck.CheckedChanged += chkCheck_CheckedChanged;
}
private void chkCheck_CheckedChanged(object sender, EventArgs e)
{
lblTest.Enabled = false;
}

Resources