Recover current value in checkBox during gridView Editing - WebForm C# - 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.

Related

Why checkbox is not maintaining it's value?

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

Getting pre-selected Text in dropdown in EditItemTemplate gridview

I am not able to get preselected text in dropdown in edit template. Please see my code:
<EditItemTemplate>
<asp:DropDownList ID="droplist" runat="server">
</asp:DropDownList>
</EditItemTemplate>
c# code
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList droplist = (DropDownList)e.Row.FindControl("droplist");
droplist.DataSource = EquipmentBLL.getunitdrop();
droplist.DataTextField = "UnitName";
droplist.DataValueField = "UnitID";
droplist.DataBind();
droplist.Items.Insert(0, new ListItem(" Select Unit ", "0"));
//droplist.Items.FindByText(unittypetext).Selected = true;
}
}
}
Can someone tell me what I should do to get preselected dropdown?
regards
Hussain
Right now, you're populating the DropDownList with options from a datasource. However, you're not binding it's selected value to anything.
Whatever you are doing to bind the other fields in your Gridview, do that for the DropDownList's SelectedValue as well.
Without seeing the rest of your GridView markup, I'm thinking something like this should work:
<EditItemTemplate>
<asp:DropDownList ID="droplist" runat="server"
SelectedValue='<%# Bind("UnitID") %>' >
</asp:DropDownList>
</EditItemTemplate>
Where "UnitID" above is the name of the field from your GridView's datasource that you want to bind to the SelectedValue of the DropDownList.

Strategy - Gridview to Excel export full records

I know there are a bunch of questions with detailed instructions on exporting from gridview to excel, but I can't find my particular situation.
I have a gridview that displays records with five fields from a search. Users can check in a checkbox any number of records. On a button click, I can successfully export only the checked records to Excel. Export is as HTML. I'm using the technique from Matt Berseth here: http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
I added a check for whether the record was checked by the user and this works fine.
But I have a requirement that for the checked records that are exported, the users want to see the entire records (i.e. all fields in just the selected records).
What is a good strategy to accomplish this?
I've tried retrieving all fields in the gridview and setting all but the five desired fields to not visible. Then in the export button click event, setting the fields to visible and rebinding. No luck there.
Thanks for any help.
Sounds like you should loop through the rows to see which are checked.
foreach (GridViewRow row in gridView.Rows)
{
if(row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = row.FindControl("CheckBoxID") as CheckBox;
if(cb != null && cb.Checked)
{
// Logic here.
}
}
From there, you can export to excel using several different mechanisms, depends on your needs. One I've used in the past if you just need xls files is NPOI -- it's open source and provides a decent framework for it. Here is a link to their site:
http://npoi.codeplex.com/
-- EDIT
After reading your comments, maybe this will help.
ASPX Code:
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Checkbox Column">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxID" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Visible Column">
<ItemTemplate>
<asp:Label ID="lblVisibleColumn" runat="server" Text='<%# Eval("VisibleColumn")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hidden Column" Visible="false">
<ItemTemplate>
<asp:Label ID="lblHiddenColumn" runat="server" Text='<%# Eval("HiddenColumn")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
Backend Code:
public class MyDataColumn
{
public string visibleColumn { get; set; }
public string hiddenColumn { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dataTable = new DataTable();
List<MyDataColumn> dataColumns = new List<MyDataColumn>();
for (int i = 0; i < 10; i++)
{
dataColumns.Add(new MyDataColumn()
{
visibleColumn = string.Format("Visible Column {0}", i),
hiddenColumn = string.Format("Hidden Column {0}", i)
});
}
gridView.DataSource = dataColumns;
gridView.DataBind();
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
List<MyDataColumn> dataColumnsToExport = new List<MyDataColumn>();
foreach (GridViewRow row in gridView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = row.FindControl("CheckBoxID") as CheckBox;
if (cb != null && cb.Checked)
{
Label lblVisibleColumn = row.FindControl("lblVisibleColumn") as Label;
Label lblHiddenColumn = row.FindControl("lblHiddenColumn") as Label;
dataColumnsToExport.Add(new MyDataColumn()
{
visibleColumn = lblVisibleColumn.Text,
hiddenColumn = lblHiddenColumn.Text
});
}
}
}
GridView gridViewToExport = new GridView();
gridViewToExport.DataSource = dataColumnsToExport;
gridViewToExport.DataBind();
//Do Something With gridViewToExport
//GridViewExportUtil.Export("GridView.xls", gridViewToExport);
}
And this should be easy to convert to a DataTable if needed -- I used my own class to make it shorter code.

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;
}

Categories

Resources