How to store into DB RadioButtonList - c#

I need to know how can i store RadioButtonList items into my SQL db, already have this for TextBoxes:
public class Botones
{
public void SaveCVInfo2(string varOne,string varTwo, string varThree)
{
using (ConexionGeneralDataContext db = new ConexionGeneralDataContext())
{
Usuario_Web columna = new Usuario_Web();
//Add new values to each fields
columna.Nombre = varOne;
columna.Apellido = varTwo;
columna.Em_solicitado = varThree;
//and the rest where the textboxes would have been
//Insert the new Customer object
db.Usuario_Web.InsertOnSubmit(columna);
//Sumbit changes to the database
db.SubmitChanges();
}
}
}
Then reference them like this:
protected void Button1_Click(object sender, EventArgs e)
{
Botones botones = new Botones();
botones.SaveCVInfo2(nombre.Text, Apellidos.Text, EmpleoS.Text);
}
I need to know a way to add to the columna data from Radio Buttons in asp.net, while keeping this format of communication with the db.
BTW i have varchar tables for RadioButtonList data in my db.
I searched for some tutorials, but only found some old fashioned ADO connections examples, and i'm using Linq to SQL here.
This is a RadioButtonList I'm using in my aspx page:
<asp:RadioButtonList ID="RadioButtonList6" RepeatColumns = "2" RepeatDirection="Vertical" RepeatLayout="Table" runat="server">
<asp:ListItem ValidationGroup="Curriculum" style="margin-right:12px; margin-top:-10px" >Si</asp:ListItem>
<asp:ListItem ValidationGroup="Curriculum" >No</asp:ListItem>
</asp:RadioButtonList>

Alright, i solved it like this.
In my class:
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Net.Mail;
namespace Grupo_Zulcon
{
public partial class EnvianosTuCurriculum : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Botones botones = new Botones();
botones.SaveCVInfo2(nombre.Text, Apellidos.Text, EmpleoS.Text, DireccionPersonal.Text, RadioButtonList6.SelectedItem.Text);
}
}
}
And in aspx.cs codebehind file:
protected void Button1_Click(object sender, EventArgs e)
{
Botones botones = new Botones();
botones.SaveCVInfo2(nombre.Text, Apellidos.Text, EmpleoS.Text, DireccionPersonal.Text, RadioButtonList6.SelectedItem.Text);
}
For anyone who might need it, thx.

Related

How to maintain view state of dynamic control linkbutton in C# asp?

I am adding dynamic LinkButton in Data Gridview, which will take me to a new page in C# asp.net. In starting, I am select value from Dropdown control. It will reload the page and display data in Gridview. In Gridview every row has dyanmic Linkbutton. When I click on Linkbutton in gridview, it is not working and page was reloaded and it hide the linkbutton.
My code is working when dropdown code pasted on page_load method.
I have researching since last 7 hours, view most of the answers but nothing is working on my problem.
As per my research, I found that after page reload LinkButton lost is state(I am not sure.).
Thanks in Advance...
using System;
using System.Web.UI.WebControls;
namespace aweFinalTTA.TTA.BatchReport
{
public partial class CenterBatchList : System.Web.UI.Page
{
protected aweFunctionNew aweApi = new aweFunctionNew();
protected aweCodePublic aweCode = new aweCodePublic();
protected LinkButton linkButton01;
protected GridView gdv;
protected DropDownList ddlTTCCode;
override protected void OnInit(EventArgs e)
{
aweApi.showForm(aweApi.getManageFieldDT("select * from manage_field where table_name='TTCBatchList'"), frmBatchList);
ddlTTCCode = (DropDownList)frmBatchList.FindControl(frmBatchList.ID + "ddlTTCCode");
ddlTTCCode.SelectedIndexChanged += new EventHandler(ddlTTCCode_SelectedIndexChanged);
frmBatchList.Controls.Add(aweApi.addGridViewNew("EmpGrid", "List of Batch Created"));
gdv = (GridView)frmBatchList.FindControl("EmpGrid");
gdv.RowDataBound += OnRowLinkBound;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
aweApi.getDDL(ddlTTCCode, "select TTCCode,TTCCode from TTCDetail where TTARNO='" + Session["TTARNO_01"].ToString() + "' and TTCDetail.TTCCode in (Select TTCCode from TTCCourseListOfTTC) and TTCDetail.TTCCode in (Select TTCCode from TTCInfraDetail) and TTCDetail.TTCCode in (Select TTCCode from TTCFacility) order by LastUpdatedDate desc", true);
TemplateField tfieldCheckbox = new TemplateField();
tfieldCheckbox.HeaderText = "Select";
gdv.Columns.Add(tfieldCheckbox);
}
}
protected void ddlTTCCode_SelectedIndexChanged(object sender, EventArgs e)
{
gdv.DataSource = aweApi.getResultsDT("select * from BatchMain where TTCCode='TTC0000002008'");
gdv.DataBind();
gdv.HeaderRow.TableSection = TableRowSection.TableHeader;
}
protected void OnRowLinkBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton linkButton = new LinkButton();
linkButton.ID = "link";
linkButton.Text = "Edit";
linkButton.ForeColor = System.Drawing.Color.Blue;
linkButton.Click += linkButton_Click;
e.Row.Cells[0].Controls.Add(linkButton);
}
}
protected void linkButton_Click(object sender, EventArgs e)
{
// GridViewRow gr = ((sender as LinkButton).NamingContainer as GridViewRow);
/// Session["TTCBatchId_01"] = gr.Cells[2].Text.Trim(); /*For first cell value of Row */
Response.Write("linkbutton_click is working");
aweCode.showMessage(this, "aaaaaa");
//Response.Redirect("~/TTA/BatchReport/BatchAddCandidate");
}
}
}

Dynamically created Dropdownlist inside Gridview doesnot fire event on second time

I have been developing a webpage which uses n number of dropdownlists which are binding dynamically inside a gridview. I want to perform operations based on the dropdownlist's selectedindexchanged event. I had done that and working good, but when I changed dropdownlist on second time It does postback but not calls the event.
You can see my code here
<%# Page Language="C#" AutoEventWireup="true" CodeFile="gridDropDownTest.aspx.cs"
Inherits="gridDropDownTest" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:GridView ID="gridLedgeDetails" runat="server" OnRowDataBound="OnRowDataBound"
OnDataBound="gridLedgeDetails_DataBound"> </asp:GridView>
</div>
</form></body></html>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class gridDropDownTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
databind();
}
public void databind()
{
DataTable dt = new DataTable();
dt.Columns.Add("Mode");
dt.Rows.Add("");
dt.Rows.Add("");
gridLedgeDetails.DataSource = dt;
gridLedgeDetails.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlMode = new DropDownList();
ddlMode.Width = 90;
ddlMode.Attributes.Add("style", "background-color:#ff6600;");
ddlMode.Items.Add("Regular");
ddlMode.Items.Add("Monthwise");
ddlMode.SelectedIndexChanged += new EventHandler(ddlMode_Indexchanged);
ddlMode.AutoPostBack = true;
ddlMode.ID = "ddlMode_";
e.Row.Cells[0].Controls.Add(ddlMode);
}
}
protected void gridLedgeDetails_DataBound(object sender, EventArgs e) { }
protected void ddlMode_Indexchanged(object sender, EventArgs e)
{
string uid = this.Page.Request.Params.Get("__EVENTTARGET");
if (uid != null && uid.Contains("ddlMode_"))
{
string[] values = uid.Split('$');
string row = values[1].Replace("ctl", "");
Control ctrl = Page.FindControl(uid);
DropDownList ddl = (DropDownList)ctrl;
if (ddl.SelectedIndex == 1)
{
}
}
}
}
enter image description here
For this you need to take and bind the dropdownlist again in Page_PreInit page method.
For Example....
protected void Page_PreInit(object sender, EventArgs e)
{
// here you need to build the gridview again.
// then the state will retain same......
}

Set focus in texbox at last char in an asp.net web page using c#

I have a textbox in an asp.net web page. In case of postback, I need to set focus on last char in this texbox. how can I do this using c# without using anything like jQueries..
Thanks
Here is a variation of the solution proposed in Use JavaScript to place cursor at end of text in text input element:
void btnPostBack_Click(object sender, EventArgs e)
{
txtFocus.Attributes["onfocus"] = "var value = this.value; this.value = ''; this.value = value; onfocus = null;";
txtFocus.Focus();
}
Your code like below :
asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ontextchanged="TextBox1_TextChanged" TabIndex="1">
asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" ontextchanged="TextBox2_TextChanged" TabIndex="2">
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Session["event_controle"] = ((TextBox)sender);
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
Session["event_controle"] = ((TextBox)sender);
}
protected void Page_PreRender(object sender, EventArgs e)
{
try
{
if (Session["event_controle"] != null)
{
TextBox controle =(TextBox) Session["event_controle"];
controle.Focus();
}
}
catch ()
{
}
}

computation after choosing on a dropdownlist

I'm creating a simple patients billing program using Asp.net c#. Now, I have a dropdownlist with items "Ward" , "Semi Private" and "Private" If the user would choose ward the textbox below it would automatically be 700, if Semi Private it should be 1000 and if Private it should be 2000. I also have a textbox under it indicating how many days the patient stayed, the user is the one who should input the days, and for example he's from the ward room and the days he stayed was 3 then the computation should be 700 * 3. I also have a textbox that will display the answer. I hope u guys understand the things I explained above. So far, here are my codes:
Default.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.Text == "Ward")
{
TextBox4.Text = "700";
}
if (DropDownList1.Text == "Semi Private")
{
TextBox4.Text = "1000";
}
if (DropDownList1.Text == "Private")
{
TextBox4.Text = "2000";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
First of all add an attribute in your asp tag for dropdownlist like-
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
Then in the event, write the following code-
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = DropDownList1.SelectedItem.ToString();
if(selected == "Ward")
{
int NetAmount = 700 * 3; //you can use any int variable in place of "3" as well
TextBox1.Text = NetAmount.ToString();
}
else if(selected == "Semi Ward")
{
TextBox1.Text = "1000";
}
else
{
TextBox1.Text = "2000"
}
}
That's it. Hope it will help.

how to assign value for drop down select item c#

i need to assign value for selected value drop down in aspx for example
dropdownlist items
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
if user select any item in dropdownlist1 it should increment value 2 then
if user select any item in dropdownlist2 it should increment value 2
i need to display total
i tried this code
static int i = 0;
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
i += 2;
Label1.Text = "hello"+i;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
i += 2;
Label1.Text = "hello"+i;
}
its working but problem is if user first select 1 in dropdown //i=2 then user select b //i=4 if user again select 1 //i=6. it should not increment if user select any value in particular drop down list. how to do it. any idea....
You're using a static variable so the i value will be kept between postbacks and will be common to all users, this is incorrect.
You need to store it in ViewState, HiddenField, or Session in order to keep the value between postbacks and also keep the value different for each user.
Here's what I would've done using ViewState:
private int Counter
{
get
{
if (ViewState["Counter"] == null)
{
return 0;
}
else
{
return (int)ViewState["Counter"];
}
}
set
{
ViewState["Counter"] = value;
}
}
private bool DropDown1Selected
{
get
{
if (ViewState["DropDown1Selected"] == null)
{
return false;
}
else
{
return (bool)ViewState["DropDown1Selected"];
}
}
set
{
ViewState["DropDown1Selected"] = value;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!this.DropDown1Selected)
{
this.DropDown1Selected = true;
this.Counter += 2;
}
Label1.Text = string.Format("hello{0}", this.Counter);
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
this.Counter += 2;
Label1.Text = string.Format("hello{0}", this.Counter);
}
Few of the answers above are talking about static variable getting reset after post back, this is incorrect, Static variables keep their values for the duration of the application domain. It will survive many browser sessions until you restart the web server Asp.net Static Variable Life time Across Refresh and PostBack
That being said, it is definitely not a good idea to use Static variables and instead go with the approaches suggested using Session or Viewstate.
About your question, I guess you want to increment the value only first time a value is chosen from the drop down list, to achieve this you would want to have a flag to let you know if the value is already selected, something on the below lines:
static bool DrpDown1;
static bool DrpDown2;
static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DrpDown1 = false;
DrpDown2 = false;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!DrpDown1)
{
i += 2;
Label1.Text = "hello" + i;
DrpDown1 = true;
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (!DrpDown2)
{
i += 2;
Label1.Text = "hello" + i;
DrpDown2 = true;
}
}
You need a temporary store like ViewState or Session to keep you values and get it back from
there.
private int GetValue()
{
return Int32.Parse(ViewState["temp"]);
}
private void SetValue(int i)
{
if(ViewState["temp"]==null)
{
ViewState["temp"]=i;
}
else
{
ViewState["temp"]= i+Int32.Parse(ViewState["temp"]);
}
}
and use it in your code as follows
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SetValue(2);
Label1.Text = string.Format("hello{0}", GetValue());
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
SetValue(2);
Label1.Text = string.Format("hello{0}", GetValue());
}

Categories

Resources