How to handle Checkbox in asp c# - c#

I have a web form in which a check box with code as below
<aspCheckBox ID="txtIsPaid" runat="server" autopostback="true"
OnCheckedChanged="txtIsPaid_CheckedChanged" />
is used.and in sql database datatype is bit.
but when i checked or unchecked this box the value is always false in database. i googled but in vain.
protected void txtIsPaid_CheckedChanged(object sender, EventArgs e)
{
if (txtIsPaid.Checked == true)
{
EmployeeLeave empleave = new EmployeeLeave();
empleave.IsPaid = txtIsPaid.Checked;
}
}

Do this to see if the value is even being changed, and take it from there:
bool isitchecked = txtIsPaid.checked;
Console.WriteLine(isitchecked);

Does your IsPaid setter already run UPDATE SQL?
It seems you don't update to database.
Maybe you must call save function, ex:
protected void txtIsPaid_CheckedChanged(object sender, EventArgs e) {
if (txtIsPaid.Checked == true) {
EmployeeLeave empleave = new EmployeeLeave();
empleave.IsPaid = txtIsPaid.Checked;
empleave.Save();
}
}

Related

GridView select - passing values to textboxes on another form

I have a GridView with the select button turned on.
<asp:CommandField ShowSelectButton="True" />
I want to pass the row values to TextBoxes that are on another page. So I am just working with on to test with the moment.
I am currently using the following code.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string Property_Name;
Property_Name = GridView1.SelectedRow.Cells[4].Text;
Session["Property_Name"] = Property_Name;
CreateSurvey CS = new CreateSurvey();
CS.PropDetails();
Response.Redirect("CreateSurvey.aspx");
This is my code from the second page (CreateSurvey.aspx)
public void PropDetails()
{
var Property_Name = Session["Property_Name"];
Create_PropName.Text = Property_Name.ToString();
}
The "CreateSurvey.aspx" page opens but the Create_PropName TextBox is empty.
Am I missing something?
try this
if you wan't to use session
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string Property_Name;
Property_Name = GridView1.SelectedRow.Cells[4].Text;
Session["Property_Name"] = Property_Name;
//you don't need this as you already set session above
//CreateSurvey CS = new CreateSurvey();
//CS.PropDetails();
Response.Redirect("CreateSurvey.aspx");
}
while receiving you just need to call below code in page load
if(Session["Property_Name"] != null)
Create_PropName.Text = Session["Property_Name"].ToString();
if you want to use query string
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("CreateSurvey.aspx?PropName="+GridView1.SelectedRow.Cells[4].Text);
}
in second page load
if(Request.QueryString["Property_Name"] != null)
Create_PropName.Text = Request.QueryString["Property_Name"];

How to prevent button double click from server side

I made disable button on javascript button click and it works in most of cases to prevent double click,but in some cases it does not work so I want to prevent double click also from server side.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
hfid.value="0";
}
}
protected void frmSubmit_Click(object sender, EventArgs e)
{
if(Convert.ToInt32(hfid.value) == 0)
{
// Code of insert
// then I set hfid.value with newly created id
}
}
<asp:Button ID="frmSubmitInsert" runat="server" OnClick="frmSubmit_Click" OnClientClick="GRVMasterAddButton_Click(this)" Text="Add"
Width="100px" Visible="false" ValidationGroup="masterGrp" />
function GRVMasterAddButton_Click(btn)
{
if (window.Page_ClientValidate('masterGrp'))
{
document.getElementById(btn.id).disabled = true;
__doPostBack(btn.id, '');
}
}
In double click scenario,two times round trip is generated.In First round trip it is ok,but in second round trip though I am setting hfid.value with newly inserted id, it shows me 0 and again duplicate record is generated.
You can use a Session variable in your frmSubmit_Click event handler to ensure server-side that you are not running the submit code twice:
In Form_Load event, add:
Session["isProcessingForm"] = false;
Modify frmSubmit_Click event handler as follows:
protected void frmSubmit_Click(object sender, EventArgs e)
{
if(Session["isProcessingForm"])
{
return;
}
Session["isProcessingForm"] = true;
if(Convert.ToInt32(hfid.value) == 0)
{
// Code of insert
// then I set hfid.value with newly created id
}
//Once form is processed
Session["isProcessingForm"] = false;
}
If this application runs on a web farm (multiple web servers), you'll need to persist session state. There are several ways to do so in ASP.NET WebForms. More info here: https://msdn.microsoft.com/en-us/library/ms178581.aspx and here: https://msdn.microsoft.com/en-us/library/ms178587.aspx
Best option is disable button in client side using javascript in very first click
Try the following: It should definitely work.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
hfid.value="0";
}
frmSubmit.Attributes.Add("onclick", "this.disabled = true; this.value = 'Loading ...'; " + ClientScript.GetPostBackEventReference(frmSubmit, null) + ";");
}
protected void frmSubmit_Click(object sender, EventArgs e)
{
//Remove following line, this is added just to test.
System.Threading.Thread.Sleep(5000);
if (Convert.ToInt32(hfid.Value) == 0)
{
// Code of insert
// then I set hfid.value with newly created id
}
}
You can use Lock. To prevent multiple call at same time.
private object _lockObj = new object();
protected void frmSubmit_Click(object sender, EventArgs e)
{
// Here we lock code. When someone else will try to access to this code
// or in your case double clicked button it will wait until _lockObj
// is set to free.
Lock(_lockObj)
{
if(Convert.ToInt32(hfid.value) == 0)
{
// Code of insert
// then I set hfid.value with newly created id
}
}
}

One GridView, two DataSources

So, I have one GridView and 2 DataSources created using "wizard" ( GridViewTasks-->NewDataSource)
I have one CheckBox on my page...when checked=true I want to use DataSource1 for my GridView, and when checked=false DataSource2.
I have tried add CheckBox_CheckedChanged event in my code-behind something like this:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked == true)
{
gvPredbiljezbe.DataSource = dsGridView1;
gvPredbiljezbe.DataBind();
}
else
{
gvPredbiljezbe.DataSource = dsGridView2;
gvPredbiljezbe.DataBind();
}
}
But this doesn't work.
Any suggestions?
I know I can go in my code-behind and do it all "manually" (SqlDataConnection->DataAdapter->DataTable->GridViewDataSource) but is there a way when you create your DataSources with GridView wizard and on CheckBox or ButtonClick event change your GridView's DataSource's?
Thanks
Best
K
Make sure to set AutoPostBack="true" for your checkbox.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
if (checkbox != null)
gvPredbiljezbe.DataSourceID = checkbox.Checked ? dsGridView1.ID : dsGridView2.ID;
}
I used to use this technique; don't assign the DatasourceID, then set it in page_init for the initial data load, then do:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked == true)
{
gvPredbiljezbe.DataSourceID = dsGridView1.ID;
gvPredbiljezbe.DataBind();
}
else
{
gvPredbiljezbe.DataSourceID = dsGridView2.ID;
gvPredbiljezbe.DataBind();
}
}
Something like that; swapping the ID of the datasource worked without issue (been a little while so I think you use ID, not ClientID property).

How does the Post back get reset ?

Let me elaborate on this... I have the code below, there is a Page_Init (which I still don't understand why it fires more than once, but that is another story), and a Page_Load and I am checking for the "isPostBack" ... everything works great while I use my controls, radio button and drop down list, as well as Buttons; however, if I press the key, even accidentally, the "isPostBack" is reset to False. What am I doing wrong? Also, my AutoEventWireup="true".
Also, this is an .ascx file.
protected void Page_init(object sender, EventArgs e)
{
LoadPageText1();
paymntpnl1.Visible = true;
curbalpnl.Visible = false;
paymntpnl2.Visible = false;
paymntpnl3.Visible = false;
paymntpnlcc.Visible = false;
}
protected void Page_Load(object sender, EventArgs e)
{
LoadPageData();
getContractInfo();
step2lbl.BackColor = System.Drawing.Color.White;
nopmt = Convert.ToDecimal(numpmts.Text);
nopmt = nopmt * nopymts2;
sb.Clear();
sb.Append("$");
sb.Append(nopmt.ToString("#.00"));
nopymts.Text = sb.ToString();
ValidateCC();
chkNewCC();
bool crdcrd = credCard;
bool newcrd = nwCard;
if (!IsPostBack){
}
}
You're checking IsPostBack but you're still doing all the resetting before the check! And then the check makes no difference because it's an empty conditional block! You should be doing this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// All the initial setup that you don't want to do again goes here.
}
// All the stuff you want to do on first load AND PostBack goes here.
}
Make sure you understand how conditionals work.

How to get ListBox to load after selecting ComboBox value?

Working in VS 2012, WinForms, C#...
I have a ListBox I would like to populate depending upon the value selected in a ComboBox. I've tested my SQL Query and it works, but I'm getting a weird problem where, when I run my routines, my ComboBox comes up empty, as well as my ListBox. When I comment out the code in my cb_Session_SelectedValueChanged routine, my CB and LB load just fine, but when it's not commented out is when my LB and CB end up blank.
This is what I have:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
private void LoadSessionListbox()
{
int tempID = Convert.ToInt32(cb_Session.SelectedValue);
// Code here to load listbox, which works without above routine.
}
Am I missing something? Why are my CB and LB blank with that first routine added?
[EDIT]:
I put the routines which were in SelectedValueChanged in a MouseClick event and it works, but not when I want it to... You have to click a couple times to get it to re-load with the correct ID. I feel like I'm getting closer, but still not the right event.
Try this:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
if(cb_Session.SelectedValue>-1)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
}
Figured it out!!
I ended up adding a simple if statement to my SelectedValueChanged routine, and it fixed everything!
private void cb_Sessions_SelectedValueChanged(object sender, EventArgs e)
{
listBox_Sessions.Visible = true;
if (cb_Sessions.SelectedValue != null)
LoadSessionListbox();
}
Works perfectly now.
Try in SelectedIndexChanged Event and follow
private void cb_Session_SelectedIndexChanged(object sender, EventArgs e)
{
if (cb_Session.SelectedValue == null) return;
if (cb_Session.SelectedIndex == -1) return;
listbox_Sessions.Visible = true;
LoadSessionListbox((int)cb_Session.SelectedValue);
}
private void LoadSessionListbox(int selectedValue)
{
//TODO: Do stuff
}

Categories