Why checkbox is not maintaining it's value? - c#

I came across an interesting problem.
I have a checkbox inside a gridview (which is inside the MODAL). Upon CLosingModal event i am fetching Checkbox but it shows FALSE, even though i check True.
Why ?
<asp:TemplateField HeaderText="Conveyed ?">
<ItemTemplate>
<asp:CheckBox ID="chkBoxIsConveyed" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsConveyed")) %>' />
</ItemTemplate>
</asp:TemplateField>
.cs
protected void btnCloseModal_Click(object sender, EventArgs e)
{
mdlLastHearingDates.Hide();
UpdateIsConveyed();
}
public void UpdateIsConveyed()
{
foreach (GridViewRow r in grdViewLastHearingDates.Rows)
{
int CaseHearingID = Convert.ToInt32(r.Cells[0].Text);
CheckBox chkBox = r.FindControl("chkBoxIsConveyed") as CheckBox;
MngCaseHearings.UpdateCasesIsConveyed(CaseHearingID, chkBox.Checked);
}
}
I debugged and it calls the functions.

Try to set AutoPostBack value to true, where I believe it does not fire the event to update the model when you checked the checkbox. For more information, you may refer to https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.autopostback(v=vs.110).aspx

Related

Recover current value in checkBox during gridView Editing - WebForm C#

I have a simple checkBox in Editable gridView :
<asp:TemplateField HeaderText="Editable">
<ItemTemplate>
<asp:Label runat="server" Text="<%# Item.IsEditable %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBoxEditable " runat="server" Text="Editable"></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateField>
When I click on edit button in the row, I would like that checkBox is already checked if value is true. (IsEditable is a boolean)
Text Field is easy because I have a BindItem on Text property in EditItemTemplate. But it's not the same for checkBox or dropdownlist
GridView
I use a UpdateItem Method to update my data in database. I tried a small condition to check my checkBox but it does'nt work.
public void GridViewRisquesAggravants_UpdateItem(IndexViewModel item)
{
try
{
if (ModelState.IsValid)
{
CheckBox chbEdit = (CheckBox)GridView.Rows[this.GridView.EditIndex].FindControl("CheckBoxEditable")
if (item.IsEditable)
chbEdit.Checked = true;
new TypeService().Update(new Type
{
IsEditable = item.IsEditable,
});
this.GridView.DataBind();
}
}
catch
{
throw;
}
}
It makes sense because I am not in the right function to declare this. But I just have 3 methods in my webform.
SelectMethod="GridView_GetData"
UpdateMethod="GridView_UpdateItem"
DeleteMethod="GridView_DeleteItem"
Where can I do this?
(And I have the same problem with datas on dropdownList. I don't know where I recover current value during editing)
Thanks in advance
(Sorry I am beginner about webforms and my english is not perfect)
Evy
use the following code instead for checkbox declaration in edit template
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean("true") %>' />
CheckBox chbx = GridView1.HeaderRow.FindControl("CheckBoxEditable") as CheckBox;
if (chbx != null && chbx.Checked)
{
//code here
}
else
{
//else condtion
}
hope this helps
Where can I do this?
Try it in RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chbEdit = (CheckBox)e.Row.FindControl("CheckBoxEditable");
string value = ((Label)e.Row.FindControl("lblID")).Text;
if (value=="True")
chbEdit.Checked = true;
else
chbEdit.Checked = false;
}
}
Note: Don't forget to add OnRowDataBound in GrindView <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
I added the property Checked=<%# BindItem.IsEditable %> on CheckBox Control and it works perfectly.

My checkbox on the gridview doesn't trigger an event

I created my gridview with checkboxes inside of it with this code.
<asp:GridView ID="GridView1" runat="server" Width="366px" autogeneratecolumn="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="SelectAllCheckBox" runat="server" AutoPostBack="true" oncheckedchanged="SelectAllCheckBox_OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="EachCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I tried check/uncheck it.
enter link description here
protected void SelectAllCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
String test = "test";
test = "newtest";
GridView1.DataSource = null;
GridView1.DataBind();
}
But it doesn't trigger any event.
enter link description here
I'm trying to find where my code is missing and searched so far but still can't.
Thank you for your help!
You must use OnItemCreated or OnItemDataBound and link your checkbox with your delegate
void Item_Created(Object sender, DataGridItemEventArgs e)
{
CheckBox cbx = (CheckBox)e.Item.FindControl("SelectAllCheckBox");
cbx.CheckedChanged += SelectAllCheckBox_OnCheckedChanged;
}
The code looks fine and works for me.
I suspect you might be binding the GridView on every postback.
When you click the CheckBox with the event attached it causes the page to refresh. If you bind the CheckBox on Page_Load (or any method that occurs on every trip to the server) it will bind the grid every time you click the CheckBox. In this case it will never get as far as firing your event.
If so, try checking for a postback before binding your GridView.
For example:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gridview1.DataSource = myDataSource;
GridView1.DataBind();
}
}

create dynamic grid with drop down

I want to create a dynamic gridview with first row as drop down on clicking edit button. I dont have any idea on how to start. Can you please help. I have gone through some articals and found using the InstantiateIn method we can achieve.
public class CreateItemTemplate : ITemplate
{
//Field to store the ListItemType value
private ListItemType myListItemType;
public CreateItemTemplate(ListItemType item)
{
myListItemType = item;
}
public void InstantiateIn(System.Web.UI.Control container)
{
//Code to create the ItemTemplate and its field.
if (myListItemType == ListItemType.Item)
{
TextBox txtCashCheque = new TextBox();
container.Controls.Add(txtCashCheque);
}
}
}
If you want to display this on a single page, you should not be creating a server control.
Use the grid's TemplateField.
Note: if you are using AutoGenerateColumns = true, just add the column to the grid markup. It will be added first.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList id="someId" runat="server">
<asp:ListItem Text="One" />
<asp:ListItem Text="twO" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
you may need to provide more information about what you want to do wth this drop down (does it need a default value?).
Based on your needs, you may be able to do this in markup, or, you may need to use a grid event.
Brian
UPDATE: adding event handler
if you set onrowcreated="GridView1_RowCreated" in the grid
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
onrowcreated="GridView1_RowCreated">
and do this in your code behind:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dropdown = e.Row.FindControl("someId") as DropDownList;
//dropdown.DataSource= <>; bind it
//dropdown.SelectedValue =<>"; / set value how you would
}
}
you can manipulate the drop down ad it gets created.
If it cannot fin the controls look in each cell: e.Row.Cells[[index]].FindControl(""someId"")

Accesing a CheckBox that's inside a Repeater

In my repeater's ItemTemplate I've a CheckBox and a disabled TextBox, I need to implement this idea: TextBox only gets enabled if the the CheckBox is checked .. so I set the CheckBox AutoPostBack to true and I tried to put this code in ItemDataBound. but I can't find my control which is weird because I use the same code but in loop "MyRptr.Item[i].FindControl...." and it works! .. I don't want to loop through all the Items, I just wish If I can know the Item number or location in which the CheckBox was created. and I've also tried to create an event handles for the CheckBox's CheckedChanged event but I can't find the CheckBox either!
protected void MyRptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
CheckBox ChkBx = e.Item.FindControl("IsSelected_ChkBx") as CheckBox;
if (ChkBx.Checked == true)
{
TextBox TxtBx = e.Item.FindControl("Value_TxtBx") as TextBox;
TxtBx.Enabled = true;
}
}
<asp:Repeater ID="MyRptr" runat="server"
onitemdatabound="MyRptr_ItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="IsSelected_ChkBx" runat="server" Text='<%# Eval("Item") %>' AutoPostBack="True" OnCheckedChanged="IsSelected_ChkBx_CheckedChanged" />
<asp:TextBox ID="Value_TxtBx" runat="server" Enabled="false"></asp:TextBox>
<asp:HiddenField ID="ID_HdnFld" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
<SeparatorTemplate>
<br></br>
</SeparatorTemplate>
</asp:Repeater>
So basically I need a clean and simple way to implement my logic and If I could get an explanation for what's happening it would be great, so any ideas =) ?
You can find your textbox as follow, but I think its better use the jQuery instead of server-side event
protected void IsSelected_ChkBx_CheckedChanged(object sender, EventArgs e)
{
var ch = (CheckBox)sender;
var txt = ch.Parent.FindControl("Value_TxtBx") as TextBox;
}

disable delete button if value in database table's--> column is true or checked

Ok... I have a database table called employees..
This has columns called ID, Name, datejoined and Cannotbedeleted (check boxes)...
Now i want to add a delete column which deletes the rows when clicked.
But there are some entries which cannot be deleted if the Cannotbedeleted field is true (checked)... so the delete button should be invisible for these rows.
Please tell me a way of how to do this...
<asp:CheckBoxField DataField="CannotBeDeleted" HeaderText="CannotBeDeleted"
SortExpression="CannotBeDeleted" />
<asp:BoundField DataField="TimeAdded" HeaderText="TimeAdded"
SortExpression="TimeAdded" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I also tried using in delete template field...
' >
But i did not know what to do in code behind??
protected bool GetShowDeleteButton()
{
}
The solution is below.... but is there a way i can refresh the page once i click the delete button in the gridview....
Try looping through the rows in GridView_DataBound, and hide the button for each row that has the checkbox checked.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach(GridViewRow myRow in GridView1.Rows)
{
//Find the checkbox
CheckBox ckbox1 = (CheckBox)myRow.FindControl("nameOfCheckBox");
if(ckbox1.Checked)
{
//Find the Delete linkbutton and hide it
LinkButton deleteButton = (LinkButton)myRow.FindControl("nameOfDeleteLinkButton");
deleteButton.Visible = false;
}
}
}
Now, here's the difference you need:
Implement the CheckBox column as a TemplateField with a CheckBox in it. You can bind the CheckBox to the data from your datasource.
Do it like this:
if($row['cannotbedeleted'] == true) {
$buttonDisplay = 'none';
}
echo "<button onclick='delete();' style='display: $buttonDisplay;'>Delete</button>";
Well that would be the solution in PHP but if the style of the button has "display: none;" in it the button will be hidden. :) HTH

Categories

Resources