I have a username TextBox and a Label which should update to (V or X) when the TextBox text is changed. The label is updated only if, for example I press a button which automatically refreshes the page.
Here is the code:
<asp:TextBox ID="username" runat="server" OnTextChanged="checkUsername" Width="80%"></asp:TextBox>
<asp:Label ID="usernameCheck" runat="server" CssClass="checkL"></asp:Label>
And the aspx.cs
protected void checkUsername(object sender, EventArgs e)
{
if (username.Text.Length < 3 || username.Text.Length > 15)
{
//---Label = X (in red)
usernameCheck.Text = "\u2715";
}
else
{
if (myBl.checkUsername(Convert.ToString(username)))
{
//---Label = X (in red)
usernameCheck.Text = "\u2715";
}
else
{
//---Label = V (in green)
usernameCheck.Text = "\u2713";
}
}
}
Thanks for any help.
You need to add AutoPostBack="true" to your TextBox. This will cause it to post back and for that server side event to fire.
There are much better ways of doing what you are trying to accomplish though, most of which do not require a full page postback. I would try to make an AJAX call using the javascript change event, and using something like a callback method.
Related
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 am trying to implement validations on textbox like if null then display a message to fill the empty field and to check the length of the text entered. I have written the code for it inside TextBox_TextChanged event but it's not working. The event is not getting fired and the user can is able to signup without a username, that is my problem at the moment. Do I have to trigger the event manually? Here is a glimpse of what I am doing:
protected void FirstN_TextBox_TextChanged(object sender, EventArgs e)
{
String firstNameEntered = FirstN_TextBox.Text;
if (firstNameEntered != null)
{
if (firstNameEntered.Length <= 20)
{
MessageBox.Show("Inside text box");
}
else
{
}
}
else
{
FirstN_TextBox.Focus();
MessageBox.Show("Please fill the marked field");
}
change it like this:
From:
<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged"></asp:TextBox>
To:
<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged" AutoPostBack ="true"></asp:TextBox>
I wanted to add some controls by clicking on the button. and when click again, this controls repeated. and then by click to another button save value from all controls.
so I use a usercontrol(FilterParameters ) that have this controls :
<div class="main">
<asp:label ID="FilterByParam" runat="server" Text="filter" ></asp:label>
<asp:TextBox ID="txtFilter" runat="server" CssClass="NormalTextBox" Width="200px"></asp:TextBox>
</div>
<div class="main">
<asp:label ID="FilterRule" runat="server" Text="Rule"></asp:label>
<asp:TextBox ID="txtRule" runat="server" Width="330px" Height="50px" TextMode="MultiLine"></asp:TextBox>
</div>
and I use this code in my code to load this user control( I add a placeholder to add this usercontrol) :
protected void lnkAddNew_Click(object sender, EventArgs e)
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
count++;
ViewState["count"] = count;
CreateControls();
}
private void CreateControls()
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
while (placeholder.Controls.Count < count)
{
Common.FilterParameters fp = (Common.FilterParameters)LoadControl("~/Common/FilterParameters.ascx");
fp.ID = "fp" + placeholder.Controls.Count.ToString();
placeholder.Controls.Add(fp);
}
}
This control load carefully. but I have problem by save data from this controls. I use this code but didn't work. ( placeholder.controls.count is always 0)
private void SaveFilters()
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
string str=string.Empty;
for (int i = 0; i <= placeholder.Controls.Count; i++)
{
Common.FilterParameters fp = (Common.FilterParameters)placeholder.FindControl("fp"+i.ToString());
if(fp!=null)
{
string param = fp.filterparamText;
string rule = fp.RuleText;
str+="{"+param+"+"+rule+"}";
}
}
if(!string.IsNullOrEmpty(str))
{
save(str);
}
}
how can I get data from this user control?
I assume your "SaveFilters" method is being called when the webform posts back, either from the Page Load event or from the event that caused the postback (eg a button click event).
You need to reload all of your dynamic controls on every postback if you want to be able to read their values. The typical pattern is to add dynamic controls from the Page Init event. The Asp.Net runtime assigns values to the controls from the form after Page Init. You can read those values from the Page Load event or from the event that caused the postback (eg a button click event).
If you do not re-add the dynamic controls the Asp.net runtime has nowhere to place the values from the form, and so those values are lost.
So, for dynamic controls:
1. Load them in the Page Init event every time the page posts back.
2. Read values from them in the Page Load event or in the event that caused the postback (eg a button click event).
How do I determine if the checkbox is checked or not checked?
Very perplexed why this is not working - it is so simple!
On my web form:
<asp:CheckBox ID="DraftCheckBox" runat="server" Text="Save as Draft?" />
<asp:Button ID="PublishButton" runat="server" Text="Save" CssClass="publish" />
Code behind which runs in the click event for my save button:
void PublishButton_Click(object sender, EventArgs e)
{
if (DraftCheckBox.Checked)
{
newsItem.IsDraft = 1;
}
}
When debugging it never steps into the If statement when I have the checkbox checked in the browser. Ideas?!
I think there maybe some other code affecting this as follows...
In Page_load I have the following:
PublishButton.Click += new EventHandler(PublishButton_Click);
if (newsItem.IsDraft == 1)
{
DraftCheckBox.Checked = true;
}
else
{
DraftCheckBox.Checked = false;
}
newsItem is my data object and I need to set the checkbox checked status accordingly.
When the save button is hit I need to update the IsDraft property based on the checked status of the checkbox:
void PublishButton_Click(object sender, EventArgs e)
{
if (IsValid)
{
newsItem.Title = TitleTextBox.Text.Trim();
newsItem.Content = ContentTextBox.Text.Trim();
if (DraftCheckBox.Checked)
{
newsItem.IsDraft = 1;
}
else
{
newsItem.IsDraft = 0;
}
dataContext.SubmitChanges();
}
}
So, isDraft = 1 should equal checkbox checked, otherwise checkbox should be un-checked. Currently, it is not showing this.
Specify event for Button Click
<asp:Button ID="PublishButton" runat="server" Text="Save" onclick="PublishButton_Click" />
What i can see you have not got a OnClick on your button. So like this:
<asp:CheckBox ID="DraftCheckBox" runat="server" Text="Save as Draft?" />
<asp:Button ID="PublishButton" runat="server" OnClick="PublishButton_Click"
Text="Save" CssClass="publish" />
And then the function should work like it is:
protected void PublishButton_Click(object sender, EventArgs e)
{
if (DraftCheckBox.Checked)
{
newsItem.IsDraft = 1;
}
}
Please replace code as following code..
void PublishButton_Click(object sender, EventArgs e)
{
if (DraftCheckBox.Checked==True)
{
newsItem.IsDraft = 1;
}
}
Try adding onclick="PublishButton_Click" in the button field on the form. And I don't know if it makes a difference, but generated event handlers are protected void.
For me the best solution in the end has been to create 2 separate pages: 1 for editing a news articles & 1 for a new news article. So Ill never then be in the position of a new news data object being created when the page reloads.
Both page return to the article index list page when the save button is pressed and that seems to work with being able to save the state of the draft checkbox and then show the state on the edit page.
The checkbox.checked isn't used in the context you want it to (this is a boolean that if true, will make the checkbox look checked).
What you could do is to use instead a checkboxlist. Then you could do the following:
foreach(Listitem li in CheckBoxList1.Items)
{
if (li.Selected)
{
NewsItem.Isdraft = 1;
}
}
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