I'm having a problem with this: in my source i gave an id and runat to my <p> tag as this :
<p style="border-style:solid" id="p1" runat="server" > Hello</p>
But when i try to run this code :
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
p1.Style.Add(HtmlTextWriterStyle.BorderColor,"" + DropDownList2.SelectedItem.Text + "")
}
and also this doesn't work:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
p1.Style.Add(HtmlTextWriterStyle.BorderColor,"Blue");
}
Please help?
Make sure your DropDownList has AutoPostback = true
To add a little: buttons such as LinkButton and Button by default will cause postbacks, but other input items like CheckBox and DropDownList do not be default. Input items that don't postback by default will have an AutoPostBack property that can be set to true to force a postback when they are changed in some way by the user.
Without setting the AutoPostBack property, ASP.NET won't be notified that the user did something, and your events (in your code-behind) will never fire.
Related
Do web controls ever appear like you are changing their values but actually retain the previous value?
I created a pop-up modal for users to edit an item. When the user clicks edit on an item on the main page, the following sequence happens:
The item's ID is passed to the Page_Load event of the modal page, and is used to populate the page control's with the item's data.
The user changes a value in a control. Ex: Changes text in a TextBox contol.
The user clicks save, triggering the Click event which creates a DataTransferObject with the values in the textboxes, which will be stored.
However, on step 3, the control's new value (TextBox.Text) still holds the value that it orginially had, not the value the user put in.
Add.aspx:
<%# MasterType VirtualPath="../MasterPages/Popup.Master" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="TextBoxDescription" runat="server"></asp:TextBox>
<telerik:RadButton ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>
</asp:Content>
Add.aspx.cs
//Cannot access the new values here
protected void btnSave_Click(object sender, EventArgs e)
{
//This will print the new text on Create, but the old text on Edit
System.Diagnostics.Debug.WriteLine(TextBoxDescription.Text);
}
//works properly
protected void Page_Load(object sender, EventArgs e)
{
objIDParam = Convert.ToInt64(Request.QueryString["ObjectID"]);
editMode = (objIDParam != 0) ? true : false;
if(editMode)
PopulateFields(objID);
}
//works properly
private void PopulateFields(long objID)
{
MyObject obj = GetObjectByID(objID);
TextBoxDescription.Text = obj.Description;
}
It is worth noting that this popup page is used for both creating items AND editing items. Create works fine (i.e. The item isn't saved with all blanks, but rather the user input). Editing an item will properly pull all that data back in, and let the user edit the fields, however I can't access the changed values in my code.
You need to check for IsPostBack in the Page_Load method.
The Page_Load gets called before the btnSave_Click method, so the TextBoxDescription.Text is getting reset to obj.Description before the btn_Save method runs.
Try returning out of Page_Load if you're posting back:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
objIDParam = Convert.ToInt64(Request.QueryString["ObjectID"]);
editMode = (objIDParam != 0) ? true : false;
if(editMode)
PopulateFields(objID);
}
Have a look at ASP.NET Page Life Cycle Overview for more info.
On my aspx page there is a RadioButtonList and it contains list items named “additions” and “termination”. Also there two gridview “GV_addition” and “GV_termination”. On selecting the radio button “addition”, then the gridview “GV_addition” will be shown. When item “termination” is selected, then the gridview “GV_termination” will be shown.
On radio button changed event, it is working correctly. But my problem is , when I click browser back button, the radio button selection and corresponding grid view not showing correctly. I got issue, “Gv_termination” is showing when radio button “addition” is selected. This issue is showing only when I click browser back button.
Please find my below code
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="addition" Value="addition"> </asp:ListItem>
<asp:ListItem Text="termination" Value="termination"></asp:ListItem>
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedIndex == 0)
{
GV_addition.Visible = true;
GV_termination.Visible = false;
}
else
{
GV_termination.Visible = true;
GV_addition.Visible = false;
}
}
I could not understand why the why the radio button selection and corresponding gridview visiblity is not working on the browsers back button event. Can any one help me to solve this issue?
Please try below steps. For me the same scenario worked.
I could see the values are updating correctly, but the updated radio checked states are not updating in to the UI. So I have set it using javacript on the Onload event of a body.
Add the Onload event on the body.
<body onload="setRadioButonStatus()">
and add the following Javascript.
function setRadioButonStatus() {
var rbtn1 = document.getElementById('RadioButton1');
var rbtn2 = document.getElementById('RadioButton2');
if (rbtn1.hasAttribute("checked"))
rbtn1.checked = true;
if (rbtn2.hasAttribute("checked"))
rbtn2.checked = true;
}
Seems like you aren't creating a "default" scenario. You can do this, and tie the logic together more closely by doing something like this on every Page_Load and leaving it out of your event.
protected void Page_Load(object sender, EventArgs e)
{
GV_addition.Visible = RadioButtonList1.SelectedIndex == 0;
GV_termination.Visible = RadioButtonList1.SelectedIndex == 1;
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
I have a radcombobox,I want get checked items and save it in database but when i click save button,page is load again and my radcombobox become empty and then all of my checked items disappear.please help me,how can keep ckeckeditems?
As D Stanley mentioned in the comments, you're probably not checking for a postback when populating your dropdown.
This is the general approach you need to use in your code...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateTheDropdown();
}
}
private void PopulateTheDropdown()
{
// Populate / databind your dropdown here
}
This will ensure your dropdown is not rebound when a postback occurs so you don't lose the selected value(s).
If you have autopostback activated you must save the selected value separately. Try to explicitly disable and check the value when the event fires:
<telerik:RadComboBox ID="RadComboBoxControl" AutoPostBack="false" OnSelectedIndexChanged="RadComboBoxControl_SelectedIndexChanged" runat="server" EmptyMessage="Select something"></telerik:RadComboBox>
protected void RadComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Only test
var seleccionado = RadComboBoxControl.Items.FindItemByText(e.Text);
}
Check if you have assigned DataSource of the control in other parts of the code
RadComboBoxControl.DataSource = ...
RadComboBoxControl.DataBind();
This will also lose the selected element
I have spend two days trying to figure out the solution to this problem, even tried ExpertExchange and still I can't get a solution. I am a very novice programmer to ASP.Net (using C#) and I DON'T want to use a string/url post
I have a MasterPage of which has a textbox called tbSearchString. It is a simple box that a user can enter something and then it does a Postback to another page SearchResults.aspx So I also have other pages, like Default.aspx that uses the MasterPage.
I have tried nearly everything and have read nearly every post I could find on the net and no mater what the Variables are always Null.
I have use this code on the searchResults loadpage event and Every one of these variables are null, even though I enter a value in the page text box and click the button to postback to the SearchResults page, the only time it works is if I am on the searchResults page and submit.
SearchResults back end page
protected void Page_Load(object sender, EventArgs e)
{
TextBox SearchString;
TextBox SearchString2;
TextBox SearchString3;
TextBox SearchString5;
if (Page.PreviousPage != null) //This is true on every test
{
SearchString = (TextBox)Page.PreviousPage.Master.FindControl("tbSearchString");
SearchString2 = (TextBox)PreviousPage.Master.FindControl("tbSearchString");
SearchString3 = (TextBox)Master.FindControl("tbSearchString");
TextBox LoginControlx = (TextBox)PreviousPage.FindControl("Form1");
if (LoginControlx != null)
{
TextBox SearchString4 = (TextBox)LoginControlx.FindControl("tbSearchString");
}
}
MainWebsite.Master page Code
<asp:TextBox ID="tbSearchString" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch1" runat="server" Text="Search" PostBackUrl="~/SearchResultRentalEquiptment.aspx" />
I don't have anything in the CS backend page
So on the Default.aspx page
nothing special Just the Masterpage and some text content, I enter some text in the textbox goes to the SearchResults page and I can not get the darn value from the Textbox control from the Default or any other page.
What say you wise ones?
how do you redirect your form to search result form? if you are using Response.redirect, the value under Page.PreviousPage.Master.FindControl will be null . Try to use Server.Transfer to see if it works.
Here's one way:
Assuming this is your Master Page code:
<asp:TextBox ID="searchbox" runat="server" /><br />
<asp:Button ID="sendSearch" runat="server" PostBackUrl="~/Results.aspx" Text="Search" />
At the end of the day, it's all about HTTP POST, so in the target page Results.aspx PageLoad:
protected void Page_Load(object sender, EventArgs e)
{
string _foo = Request.Form[this.Master.FindControl("searchbox").UniqueID];
}
Hth....
Check out Session. I use it all the time when I need to get data from one page to another. I'm not currently able to write out a full example for you, but off the top of my head the following should work:
//page1.aspx:
protected void btnSubmit_Click(object sender, EventArgs e)
{
string greetingString = "Hello";
Session["MyValue"] = greetingString;
Response.Redirect("page2.aspx");
}
//page2.aspx:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Session["MyValue"].ToString()); //prints "Hello"
}
I am creating a custom view in a SharePoint visual Web Part using ASP.NET (Visual C#) and have a CheckBoxList, and a button.
MarkUp for the List & Button:
<td>
<asp:checkboxlist ID="cblYearLst" runat="server" EnableViewState="true" />
</td>
<td>
<asp:Button ID="btnRefineSearch" Text="Refine Search" runat="server" />
</td>
I add items to the CheckBoxList on PreRender:
if (!IsPostBack)
{
if (LstYears != null)
{
for (int i = 0; i < LstYears.Count(); i++)
{
cblYearLst.Items.Add(new ListItem(LstYears[i], LstYears[i]));
}
}
}
And I call the event Handler for the button on Page_Load:
btnRefineSearch.Click += new EventHandler(this.btnRefineSearch_Click);
All of the CheckBox list-items do not stay selected after the button is clicked. I can retrieve the selected values, but they won't display as selected. When I add the Click event handler for the button in the pre-render event, the data is displayed appropriately but the selected values can no longer be retrieved by my Click event.
Any ideas on what might be causing this behaviour??
Did you try moving the binding of the checkboxlist into the page_load instead of pre_render? Just an idea because it seems like the page is losing selections on postback and you are regenerating the options each time.
UPDATE: I created a quick page and this works correctly. Do you have your viewstate turned off for the entire page in your page directive, or possibly in the web.config? I see you have it enabled on the checkboxlist but maybe there is a global setting throwing you off.
protected void Page_Load(object sender, EventArgs e)
{
btnRefineSearch.Click += new EventHandler(this.btnRefineSearch_Click);
List<string> LstYears = new List<string>();
LstYears.Add("one");
LstYears.Add("two");
LstYears.Add("three");
LstYears.Add("four");
if (!IsPostBack)
{
if (LstYears != null)
{
for (int i = 0; i < LstYears.Count; i++)
{
cblYearLst.Items.Add(new ListItem(LstYears[i], LstYears[i]));
}
}
}
}
private void btnRefineSearch_Click(object sender, EventArgs args)
{
Response.Write(cblYearLst.SelectedValue);
}
I figured out the issue, since I have AutoEventWireUp set to true as Keenan has suggested it should all work if I do it in the page_load.
The problem was that Page_Load was being called twice, and I discovered that this was because I was redirecting the user to the same URL with QueryString parameters. After I made the much needed changes, my code works very well.
+1 Keenan for your help and thank you (#jfmags) for tipping me off by telling me you think it is something else.
:D