I want to enable and disable textfields on checked change event but this event is not firing.
Following is my radio button code
<asp:RadioButton ID="submitter" runat="server" GroupName="rd2" OnCheckedChanged="submitter_CheckedChanged" Text="Submitter" />
<asp:RadioButton ID="following" runat="server" ForeColor="Black" Text="Following" GroupName="rd2" OnCheckedChanged="following_CheckedChanged" />
Following is my checked change event code
protected void following_CheckedChanged(object sender, EventArgs e)
{
if (submitter.Checked == true)
{
contactName.Enabled = false;
Contactmail.Enabled = false;
Response.Write("<script LANGUAGE='JavaScript' >alert('following event submitter check')</script>");
}
if (following.Checked == true)
{
contactName.Enabled = true;
Contactmail.Enabled = true;
Response.Write("<script LANGUAGE='JavaScript' >alert('following event following check')</script>");
}
}
protected void submitter_CheckedChanged(object sender, EventArgs e)
{
if (submitter.Checked == true)
{
contactName.Enabled = false;
Contactmail.Enabled = false;
Response.Write("<script LANGUAGE='JavaScript' >alert('submitter event submitter check')</script>");
}
if (following.Checked == true)
{
contactName.Enabled = true;
Contactmail.Enabled = true;
Response.Write("<script LANGUAGE='JavaScript' >alert('submitter event following check')</script>");
}
Please tell me where is problem. thanks
set AutoPostBack="true"
Postback means sending Data to server. It will execute server side events.
for more Details Click Here
<asp:RadioButton ID="submitter" runat="server" GroupName="rd2" OnCheckedChanged="submitter_CheckedChanged" Text="Submitter" AutoPostBack="true" />
<asp:RadioButton ID="following" runat="server" ForeColor="Black" Text="Following" GroupName="rd2" OnCheckedChanged="following_CheckedChanged" AutoPostBack="true" />
You need to add AutoPostBack="true".
Change the aspx code to as below:
<asp:RadioButton AutoPostBack="true" ID="submitter" runat="server" GroupName="rd2" OnCheckedChanged="submitter_CheckedChanged" Text="Submitter" />
<asp:RadioButton AutoPostBack="true" ID="following" runat="server" ForeColor="Black" Text="Following" GroupName="rd2" OnCheckedChanged="following_CheckedChanged" />
Related
Error on RowUpdating >> GridView 'GView' fired the RowUpdating event, which was not handled.
I put the RowEditing and I can enter to update the data, but when I put public void GView_UpdateItem(int id) {} it returns the error saying GridView 'GView' triggered the RowUpdating event, which was not handled.
<asp:GridView runat="server" ID="GView" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing="GView_RowEditing" UpdateMethod="GView_UpdateItem">
<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
<asp:CommandField ShowDeleteButton="True"></asp:CommandField>
</Columns>
C# CODE
protected void Page_Load(object sender, EventArgs e)
{
}
public static List<Contato> contatos = new List<Contato>();
public void btnCad(object sender, EventArgs e)
{
Contato contato = new Contato();
contato.Id = contatos.Count;
contato.Nome = this.txtNome.Text;
contato.Idade = this.txtIdade.Text;
contato.Telefone = this.txtTelefone.Text;
contato.Genero = this.txtGenero.Text;
contato.DataCadastro = this.txtDataCad.Text;
contatos.Add(contato);
GView.DataSource = contatos;
GView.DataBind();
}
public void GView_UpdateItem(int id)
{
VoxTraining.Models.Contato item = contatos.Find(cont => cont.Id == id);
if (item == null)
{
ModelState.AddModelError("", String.Format("O item com id {0} não foi encontrado", id));
return;
}
TryUpdateModel(item);
if (ModelState.IsValid)
{
}
}
protected void GView_RowEditing(object sender, GridViewEditEventArgs e)
{
return ;
}
Ok, a few things.
Yes, we can do this without a database. We have have to persist our "list of names".
Dump the delete and edit buttons for the GV - they not worth the trouble - they don't help you.
So, we have the edit area, and the grid.
So, we have this markup:
<div id="myedit" runat="server" style="width:30%;padding:35px;display:none">
<div cssclass="form-group">
<label for="exampleInputEmail1">Nome</label>
<asp:TextBox CssClass="form-control" runat="server" ID="txtNome"></asp:TextBox>
</div>
<div cssclass="form-group">
<label for="exampleInputPassword1">Idade</label>
<asp:TextBox CssClass="form-control" runat="server" ID="txtIdade"></asp:TextBox>
</div>
<div cssclass="form-group">
<label for="exampleInputPassword1">Telefone</label>
<asp:TextBox CssClass="form-control" runat="server" ID="txtTelefone"></asp:TextBox>
</div>
<asp:Label runat="server" AssociatedControlID="txtGenero" ><b>Genero</b></asp:Label><br />
<asp:DropDownList CssClass="form-group" checked="" ID="txtGenero" runat="server" OnSelectedIndexChanged="btnCad">
<asp:ListItem Text="Masculino" />
<asp:ListItem Text="Feminino" />
</asp:DropDownList>
<div cssclass="form-group">
<label for="exampleInputPassword1">Data de Cadastro</label>
<asp:TextBox CssClass="form-control" runat="server" TextMode="Date" ID="txtDataCad"></asp:TextBox>
</div>
<br />
<asp:Button runat="server" ID="IDbtnCad" CssClass="btn" Text="Save" OnClick="btnCad" />
<asp:Button runat="server" ID="btnCancel" CssClass="btn" Text="Cancel" OnClick="btnCancel_Click"
style="margin-left:20px" />
<asp:Button runat="server" ID="btnDelete" CssClass="btn" Text="Delete" OnClick="btnCancel_Click"
style="margin-left:30px" />
</div>
<div id="MyGrid" runat="server">
<asp:Button ID="cmdNew" runat="server" Text="+Add new" CssClass="btn" OnClick="cmdNew_Click"/>
<asp:GridView runat="server" ID="GView" CellPadding="4" ForeColor="#333333" cssclass="table" Width="40%"
ShowHeaderWhenEmpty="true" >
<Columns>
<asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="cmdEdit" runat="server" Text="Edit" CssClass="btn" OnClick="cmdEdit_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"></HeaderStyle>
</asp:GridView>
</div>
And code to load is this:
List<Contato> MyNames = new List<Contato>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["MyNames"] = MyNames;
GView.DataSource = MyNames;
GView.DataBind();
}
else
MyNames = ViewState["MyNames"] as List<Contato>;
}
Note how we save the list of names.
So, we now have this:
so, add new button is thus this:
protected void cmdNew_Click(object sender, EventArgs e)
{
// add new
Contato OneName = new Contato();
MyNames.Add(OneName);
MyEdit(MyNames.Count - 1);
}
And my edit is this:
void MyEdit(int RowIndex)
{
Contato OneName = MyNames[RowIndex];
txtNome.Text = OneName.Nome;
txtIdade.Text = OneName.Idade;
txtTelefone.Text = OneName.Telefone;
txtGenero.Text = OneName.Genero;
txtDataCad.Text = OneName.DataCadastro;
ViewState["RowIndex"] = RowIndex;
// show editor, hide grid
myedit.Style.Add("Display", "nomral");
MyGrid.Style.Add("display", "none");
}
so, we hit add, and we now have this:
We hit save button, this code
protected void btnCad(object sender, EventArgs e)
{
int RowIndex = (int)ViewState["RowIndex"];
Contato OneName = MyNames[RowIndex];
OneName.Nome = this.txtNome.Text;
OneName.Idade = this.txtIdade.Text;
OneName.Telefone = this.txtTelefone.Text;
OneName.Genero = this.txtGenero.Text;
OneName.DataCadastro = txtDataCad.Text;
ViewState["MyNames"] = MyNames;
myedit.Style.Add("Display", "none");
MyGrid.Style.Add("display", "normal");
GView.DataSource = MyNames;
GView.DataBind();
}
And we now have this:
We can use add again, and now this:
the edit row button - plane jane asp.net button click.
Code is this:
protected void cmdEdit_Click(object sender, EventArgs e)
{
Button btnEdit = sender as Button;
GridViewRow gRow = btnEdit.NamingContainer as GridViewRow;
MyEdit(gRow.RowIndex);
}
So, hitting edit button, goes back to the editor.
Say like this:
Cancel button - does nothing (no save, return to grid).
Say this:
protected void btnCancel_Click(object sender, EventArgs e)
{
myedit.Style.Add("Display", "none");
MyGrid.Style.Add("display", "normal");
}
And the delete button - that would just remove the current row index item from the list.
eg:
protected void cmdDel_Click(object sender, EventArgs e)
{
myedit.Style.Add("Display", "none");
MyGrid.Style.Add("display", "normal");
int RowIndex = (int)ViewState["RowIndex"];
MyNames.RemoveAt(RowIndex);
GView.DataSource = MyNames;
GView.DataBind();
}
I have this code
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
RadioButton2.Checked = false;
DropDownList1.Enabled = true;
}
if (!RadioButton1.Checked)
{
RadioButton2.Checked = true;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
RadioButton1.Checked = false;
DropDownList1.Enabled = false;
}
if (!RadioButton1.Checked)
{
RadioButton1.Checked = true;
}
}
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="BookStore" DataTextField="Name" DataValueField="Name" Height="51px" Width="300px" DataMember="DefaultView">
</asp:DropDownList>
<asp:SqlDataSource ID="BookStore" runat="server" ConnectionString="<%$ ConnectionStrings:BookStoreConnectionString %>" SelectCommand="SELECT [Name] FROM [Books] ORDER BY [Name]"></asp:SqlDataSource>
<p>
<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="1" OnCheckedChanged="RadioButton1_CheckedChanged" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="1" OnCheckedChanged="RadioButton2_CheckedChanged" />
</p>
But when I click radiobuttons, dropdownlist is alwais enabled. When I write this functions in webforms app(not asp.net) its working correct.What could be the reason of this uncorrect working?
May be problem in RadioButton2_CheckedChanged event in second if condition, this should check RadioButton2.Checked not RadioButton1.Checked. instead of checking this condition if you put else still this will work no need to check again.
if (!RadioButton2.Checked)
{
RadioButton1.Checked = true;
}
I have this form with a custom validator, and Button.
But, my custom Validator shows error only after button click.
This is my validator, Button and code behind.
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="User Name cannot be Empty!" ForeColor="Red"
onservervalidate="CustomValidator1_ServerValidate"
ControlToValidate="userNameTxt" ValidateEmptyText="True"
ValidationGroup="save-valid"></asp:CustomValidator>
<asp:Button ID="saveButton" runat="server" Text="Save" CssClass="save-button"
onclick="saveButton_Click" TabIndex="7" ValidationGroup="save-valid" />
This is my code behind.
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (IsUserValid(userNameTxt.Text))
{
CustomValidator1.ErrorMessage = "User Name cannot be Empty!";
args.IsValid = false;
}
else
args.IsValid = true;
}
protected void saveButton_Click(object sender, EventArgs e)
{
//This code executes regardless of CUstom Validator
}
You can try to use client side validation.
<script type="text/jscript">
function textBoxControl(source,arguments){
if(arguments.Value.length >0) //or control things (e.g. at least 6 character)
arguments.isValid = true;
else
arguments.isValid = false;
}
<script>
Code behind is like below.
<asp:TextBox ID="userNameTxt" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CustomValidator ID="CustomValidator1"
runat="server" ErrorMessage="CustomValidator"
onservervalidate="CustomValidator1_ServerValidate"
ControlToValidate="userNameTxt"
ClientValidationFunction="textBoxControl"></asp:CustomValidator>
When I put the below code inside Save Button click, the problem gets solved.
if (!Page.IsValid)
return;
I have a dropdownlist. I want whenever user selects India, it should go to the next section of the page and if it selects other than India, it should go to the some other section. And also it should only move ahead if the checkbox is checked, Otherwise it should ask to check the checkbox first. Please see the code for your reference:-
<asp:DropDownList ID="ddlCountry" runat="server" class="txtfld-popup" Width="251">
<asp:ListItem Text="Enter your Country of Residences" Value="0"></asp:ListItem>
<asp:ListItem Text="India" Value="1"></asp:ListItem>
<asp:ListItem Text="USA" Value="2"></asp:ListItem>
<asp:ListItem Text="Other" Value="3"></asp:ListItem>
</asp:DropDownList>
Also see the checkbox code:
<asp:CheckBox ID="checkcountry" runat="server" />
I confirm that I am a resident of the selected jurisdiction.
<div id="errordiv" runat="server" style="color: #cf060d; font-size: 12px;"></div>
Also, see the button code
<asp:Button ID="btnSend" runat="server" ValidationGroup="VG"
OnClick="btnSend_Click" Class="button-form" Width="65" />
On button click event
protected void btnSend_Click(object sender, EventArgs e)
{
if (!checkcountry.Checked)
{
errordiv.InnerHtml = "Please select the authorization checkbox to proceed.";
return;
}
if (Page.IsValid)
{
// Response.Redirect("LegalPopup1.aspx");
ClientScript.RegisterClientScriptBlock(this.GetType(), "ResponseDialog", "$(document).ready(function(){ResponseDialog();});", true);
}
}
Add AutoPostBack as true for the DropDownList and add selected index changed event like below
<asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true"
inside your "ddlCountry_SelectedIndexChanged" code, you can check the selected values, checkbox values and do whatever you want based on those values.
Code
protected void btnSend_Click(object sender, EventArgs e)
{
if (checkcountry.Checked == true)
{
if (checkcountry.Checked == true && ddlCountry.SelectedIndex != 0 && ddlCountry.SelectedItem.Text == "India")
{
divForInida.Visible = true;
divForOther.Visible = false;
}
else if (checkcountry.Checked == true && ddlCountry.SelectedIndex != 0 && ddlCountry.SelectedItem.Text != "India")
{
divForInida.Visible = false;
divForOther.Visible = true;
}
else
{
//message for `Select a country of residence`
}
}
else
{
//message for `check the checkbox`
}
}
<asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true" >
And Your selected index changed event will look like this
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (!string.IsNullOrEmpty(ddlCountry.SelectedItem.Text))
{
if (ddlCountry.SelectedItem.Text == "India")
{
}
else
{
}
}
}
catch (System.Exception ex)
{
throw new MyException(" Message:" + ex.Message, ex.InnerException);
}
}
I have created a simple radiobuttonlist. And I wrote simple function to do something on selection
here is the radiobuttonlist
<asp:RadioButtonList ID="payment_type" CssClass="rbl_type" runat="server" TextAlign="Right" RepeatDirection="Horizontal" BorderStyle="None" OnSelectedIndexChanged="payment_type_SelectedIndexChanged">
<asp:ListItem Value="0">Serbest Ödeme</asp:ListItem>
<asp:ListItem Value="1">Ön Tanımlı Ödeme</asp:ListItem>
</asp:RadioButtonList>
here is the c# code
protected void payment_type_SelectedIndexChanged(object sender, EventArgs e)
{
if (payment_type.SelectedValue == "0")
{
pnl_serbest.Visible = true;
pnl_on_tanimli.Visible = false;
}
else
{
pnl_serbest.Visible = false;
pnl_on_tanimli.Visible = true;
}
}
but it doesnt trigger anything. what am I doing wrong?
Add
AutoPostBack="true"
attribute to your <asp:RadioButtonList /> tag.