Get data from dynamically created controls in TemplateFields for gridview - c#

I have a webpage that has a gridview which is bound to a list of custom objects but also has 1 dynamically created TemplateFields. These fields are created at Page_PreRender and can be a textbox or dropdownlist based on the object in the list. I have a button at the bottom of the page that needs to save all the data inputed in the dynamic objects when pressed. When i try to find the dynamic control i am unable to do so using the FindControl() method. It always comes back blank.
How can i retrieve the user entered/selected data?
This is my gridview
<div id="divSearchCriteriaGrid" runat="server" class="padding-top-15">
<asp:GridView ID="gvSearchCriteria" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvSearchCriteria_OnRowDataBound" GridLines="None">
<Columns>
<asp:BoundField DataField="SearchFieldId" Visible="False" />
<asp:TemplateField HeaderText="Search Field">
<ItemTemplate>
<asp:CheckBox ID="cbDisplay" runat="server" AutoPostBack="False" onclick="ToggleCriteriaControls(this)" />
</ItemTemplate>
<ItemStyle Width="25%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Begin Criteria">
<ItemStyle Width="35%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="End Criteria">
<ItemStyle Width="35%" />
</asp:TemplateField>
<asp:BoundField DataField="ControlTypeId" Visible="False" />
</Columns>
</asp:GridView>
</div>
And this is my code behind for creating the controls:
public void Page_PreRender(object sender, EventArgs e)
{
TryAction(PrepareLoad);
}
private void PrepareLoad()
{
if (IsPostBack) return;
BindData();
}
private void BindData()
{
gvSearchCriteria.DataSource = null;
gvSearchCriteria.DataSource = SearchFieldList;
gvSearchCriteria.DataBind();
}
protected void gvSearchCriteria_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SearchField searchField = e.Row.DataItem as SearchField;
if (searchField != null)
{
e.Row.Cells[0].Text = searchField.SearchFieldId.ToString();
e.Row.Cells[4].Text = searchField.ControlTypeId.ToString();
CheckBox checkBox = (CheckBox)e.Row.FindControl("cbDisplay");
checkBox.Text = searchField.FieldDescription;
checkBox.Checked = searchField.Checked;
checkBox.Enabled = true;
if (searchField.ControlTypeId.ToString() == "1")
{
RadTextBox textBox = new RadTextBox();
textBox.ID = "txtBoxBegin";
e.Row.Cells[2].Controls.Add(textBox);
textBox = new RadTextBox();
textBox.ID = string.Format("txt{0}End", searchField.SearchFieldId);
e.Row.Cells[3].Controls.Add(textBox);
}
else if (searchField.ControlTypeId.ToString() == "2")
{
RadComboBox comboBox = new RadComboBox();
comboBox = new SearchService().GetRadComboBoxById(searchField.SearchFieldId);
comboBox.ID = string.Format("cbo{0}Begin", searchField.SearchFieldId);
e.Row.Cells[2].Controls.Add(comboBox);
}
}
}
}
This all works good and the controls get created with the checkbox.
Here is my code for trying to loop through each row of the gridview to get the user entered data which does not work.
private void Save()
{
foreach (GridViewRow row in gvSearchCriteria.Rows)
{
CheckBox include = (CheckBox)row.FindControl("cbDisplay");
int id, controlTypeId;
string criteriaOne = string.Empty;
string criteriaTwo = string.Empty;
if (!include.Checked) continue;
id = int.Parse(row.Cells[0].Text);
if (controlTypeId == "1")
{
RadTextBox radTextBox = (RadTextBox) row.FindControl("txtBoxBegin");
if (radTextBox != null)
{
criteriaOne = radTextBox.Text;
}
radTextBox = (RadTextBox)row.FindControl("txtBoxEnd");
if (radTextBox != null)
{
criteriaTwo = radTextBox.Text;
}
}
else if(controlTypeId == "2")
{
RadComboBox radComboBox = (RadComboBox)row.FindControl(string.Format("cbo{0}Begin",id));
if (radComboBox != null)
{
criteriaOne = radComboBox.SelectedValue;
}
}
}
}
The radTextBox and radComboBox variables i am trying to get using the FindControlId always comes back null.
cell 0 comes back ok each time with the correct Id. the cbDisplay checkbox always returns whether the row is checked or not and cell 4 gets the ControlTypeId just fine. It is the TemplateFields that i cannot get the values for.
Any help is greatly appreciated.

I was able to get the information from the dynamic controls once i moved the BindData() function to the Page_Init instead of the Page_PreRender().

Related

How to loop through each and every row, put value in gridview column cells

i have gridview with 3 columns and 100 rows. and one submit button out of gridview.
here first 2 column is bounded field and 3rd one is Label (templatefield) .
Now, I want to change status of 3rd column and display it to gridview when loop run.
i want to
when i=0 then i want to change label's value to "SUCESS" and display on Gridview,
i=1 then i want to change label's value to "SUCESS" and display on Gridview,
and so on till i=100.
Gridview
<asp:GridView ID="GvCategoryLive" runat="server" AutoGenerateColumns="False" EmptyDataText="No Records Found !" >
<Columns>
<asp:BoundField DataField="UnitName" HeaderText="Unit Name" />
<asp:BoundField DataField="CreateDate" HeaderText="Created Date" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="UnitMesurement_Id" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am trying something like this
protected void btnSave_Click(object sender, EventArgs e)
{
if (btnSave.Text == "Save")
{
for (int i = 0; i < GvCategoryLive.Rows.Count; i++)
{
System.Threading.Thread.Sleep(9000);
GvCategoryLive.Rows[i].Cells[2].Text = "Success";
}
}
}
I really dont remember the gridviews and asp.net webforms but
You may use setTimeout in javascript
var rowCount = 100;// get row count here
for(int i = 0; i < rowCount ; i++)
{
setTimeout($('#cell'+i).html('Success'), 9000);
}
You could try like this:
protected void btnSave_Click(object sender, EventArgs e)
{
if (btnSave.Text == "Save")
{
foreach(GridViewRow row in GvCategoryLive.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
Label UnitMesurement_Id= row.FindControl("UnitMesurement_Id") as Label ;
UnitMesurement_Id.Text = "Success";
}
}
}
}
Because you are not setting the value of a cell but label inside that cell, you should change this-
GvCategoryLive.Rows[i].Cells[2].Text = "Success";
into-
Label UnitMesurement_Id = GvCategoryLive.Rows[i].FindControl("UnitMesurement_Id") as Label;
UnitMesurement_Id.Text = "Success";

How to hide a link in a cell of a gridview?

I have a gridview which displays a data from the database. In one of the tables in this database have a column to store details about attanchment file. If the attachment is available that column value will set as "YES". Otherwise it will set as "NO". What I want to do is, show a link to view the attachment. But if cell value of the column is "NO" (when there is no attachment) the link must be hidden.
Note : I know how to view a file. Here what Im expecting is to hide the link in the cell which doesn't have an attachment.
This is what I have done upto now.
if (ds.Tables[0].Rows.Count > 0)
{
grdSo.DataSource = ds;
grdSo.DataBind();
for(int i=0; i <ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i][6].Equals("NO"))
{
grdSo.Rows[i].Cells[6].Visible = false;
}
else
{
grdSo.Rows[i].Cells[6].Visible = true;
}
}
}
I could hide the cell using this code. But unfortunately this hides the lines of the cell too. How can I avoid it happening?
One of the ways to do this is to use server side control like LinkButton to view the link that you want to show and set the visibility of the control as per your requirement in the OnRowDataBound event of the gridview.
Below is the code to show/hide LinkButton on OnRowDataBound event.
protected void gridId_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataSourceClass varData = (DataSourceClass)e.Row.DataItem;
// check if your data have flag to show the link
if(varData.show)
((LinkButton)e.Row.FindControl("linkbuttonId")).Visible = true;
else
((LinkButton)e.Row.FindControl("linkbuttonId")).Visible = false;
}
}
ASPX Code :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField Visible="false" DataField="id" />
<asp:TemplateField HeaderText="Has Attachment">
<ItemTemplate>
<asp:Label ID="lblAtt" runat="server" Text='<%#Eval("HasAtt") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Attachment">
<ItemTemplate>
<asp:LinkButton ID="lbtnAtt" runat="server" OnClick="lbtnAtt_Click" Visible="false">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS Code :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
//Here you write databind logic
// Datasource table of GridView1 should contain 'HasAtt' and 'id' as its binded to label.
}
private void ViewAttachment(int id)
{
//Here you write your logic to view attachment.
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow) // checking if row is datarow or not
{
Label lblHasAtt = e.Row.FindControl("lblAtt") as Label;
LinkButton lbtnViewAtt = e.Row.FindControl("lbtnAtt") as LinkButton;
lbtnViewAtt.Visible = (lblHasAtt.Text.ToLower() == "yes");
}
}
protected void lbtnAtt_Click(object sender, EventArgs e)
{
LinkButton lbtnViewAtt = sender as LinkButton;
GridViewRow grw = lbtnViewAtt.NamingContainer as GridViewRow;
int id = Convert.ToInt32(this.GridView1.Rows[grw.RowIndex].Cells[0].Text); // retriving value of first column on 'lbtnAtt' click row.
this.ViewAttachment(id);
}
I recall being able to parse through the grid view by using a for each for the rows and using a for statement for columns.
If I recall correctly, you can grab an item ie
row.Item[i]
foreach(GridViewRow row in GridView1.Rows)
{
for(int i = 0; i < GridView1.Columns.Count; i++)
{
// here you can do the logic to decide whether to show or hide the text for this cell
}
}
Sorry for formatting responding on my phone.

Radio Button doesn't work inside my GridView

I have a GridView. Every row has a textbox and a radiobutton (3 options)
If the radiobutton is selected then the textbox.text = ""
Problem: when OnSelectedIndexChanged is called every textbox inside my grid goes blank
How can I clear only the textbox of the row I selected the radiobutton in?
ASPX markup
<asp:GridView id="mygrid" Runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButtonList ID="hi" runat="server"
OnSelectedIndexChanged="zzz" AutoPostBack="true" />
<asp:TextBox ID="txPregoeiro" runat="server" Text="." />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C# code-behind
protected void zzz(object sender, EventArgs e)
{
foreach (GridViewRow _row in mygrid.Rows)
{
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
}
}
}
Your not checking to see if the radio button list has a selected item or not. As a result you are always setting the textbox text to blank. Change the function to:
GridViewRow _row = mygrid.SelectedRow;
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
if(hi.SelectedItem != null) //This checks to see if a radio button in the list was selected
{
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
}
}
You are currently doing it for every row which will clear every text box. Give this a try.
protected void zzz(object sender, EventArgs e)
{
var caller = (RadionButtonList)sender;
foreach (GridViewRow _row in mygrid.Rows)
{
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
if(hi == caller)
{
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
break; //a match was found break from the loop
}
}
}
}

Adding a checkbox column to asp.net gridview

I have a couple questions when it pertains to adding a CheckBox column to gridview in asp.net and getting multiple values. First off I see everyone adding OnCheckedChanged="chkview_CheckedChanged" to their aspx page but when you click on the CheckBox to set its actions it does not open OnCheckedChanged="chkview_CheckedChanged". It opens SelectedIndexChanged event instead. What I am trying to do is when they select a CheckBox it adds the corresponding rows info to the TextBox. Here is what I am currently using to set the values. How can I use a selected CheckBox instead?
protected void dropGridView_SelectedIndexChanged1(object sender, EventArgs e)
{
GridViewRow row = dropdeadGridView.SelectedRow;
IDTextBox.Text = row.Cells[1].Text;
loadnumTextBox.Text = row.Cells[2].Text;
}
Once done with that how can you make it to where it will get every row that is checked instead of just one which is what is my current problem. I am looking for a way to select multiple rows and have a select button. I have done a lot of looking and can find nothing on it so I am trying to accomplish this with CheckBoxes instead. Any ideas how I can add this and get the multiple rows that can be selected. Thank you in advance.
Here is my edit* Posting asp code for CheckBox column:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="SelectCheckBox" runat="server" OnCheckedChanged="SelectCheckBox_OnCheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
First you have to set autopostback attribute to true :
<asp:CheckBox ID="SelectCheckBox" runat="server" AutoPostBack="true"
OnCheckedChanged="SelectCheckBox_OnCheckedChanged"/>
In your case, SelectedIndexChanged is sent by the gridview. For the checkbox event you have to use OnCheckedChanged event :
protected void SelectCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox ;
if(chk.Checked)
{
GridViewRow row = (GridViewRow)chk.NamingContainer;
IDTextBox.Text = row.Cells[1].Text;
loadnumTextBox.Text = row.Cells[2].Text;
}
}
If you want to loop through all selected checkboxes :
var rows = dropdeadGridView.Rows;
int count = dropdeadGridView.Rows.Count;
for (int i = 0; i < count; i++)
{
bool isChecked = ((CheckBox)rows[i].FindControl("chkBox")).Checked;
if(isChecked)
{
//Do what you want
}
}
HTML Gridview example
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Class="table table-striped table-bordered" ShowHeaderWhenEmpty="true" HeaderStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" HorizontalAlign="Center" Height="40px" Width="80%" EmptyDataText="No Stock in The Shop"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="chkAllSelect" runat="server" onclick="checkAll(this);" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkSelect" onclick="Check_Click(this);EnableBTN(this);" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="SHOP_CODE" HeaderText="SHOP CODE" /> <asp:BoundField DataField="ITEM_CODE" HeaderText="ITEM CODE" /> <asp:BoundField DataField="ITEM_NAME" HeaderText="ITEM NAME" /> <asp:BoundField DataField="COLOR_CODE" HeaderText="COLOR CODE" /> <asp:BoundField DataField="COLOR_NAME" HeaderText="COLOR NAME" /> <asp:BoundField DataField="STOCK_NO" HeaderText="STOCK NUMBER" /> <asp:BoundField DataField="STOCK_IN_HAND" HeaderText="STOCK IN HAND" /> <asp:BoundField DataField="LOCATION_CD" HeaderText="LOCATION" /> <asp:TemplateField HeaderText="NO OF QUANTITY"> <ItemTemplate> <asp:TextBox CssClass="form-control" onkeyup="this.value=this.value.replace(/[^0-9]/g,'');" Placeholder="Enter The Correct Qty" ID="ADJqty" runat="server" AutoCompleteType="Disabled" Text=""></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Use this function check all the gridview check box
function checkAll(objRef) {
var GridView = objRef.parentNode.parentNode.parentNode;
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
//Get the Cell To find out ColumnIndex
var row = inputList[i].parentNode.parentNode;
if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
if (objRef.checked) {
//If the header checkbox is checked
//check all checkboxes
//and highlight all rows
row.style.backgroundColor = "#e3f1ff";
inputList[i].checked = true;
$('#DummyBTNAUTHLeave').prop('disabled', false);
$('#DummyBTNRJTLeave').prop('disabled', false);
}
else {
//If the header checkbox is checked
//uncheck all checkboxes
//and change rowcolor back to original
if (row.rowIndex % 2 == 0) {
//Alternating Row Color
row.style.backgroundColor = "rgba(0,0,0,.05)";
}
else {
row.style.backgroundColor = "white";
}
inputList[i].checked = false;
$('#DummyBTNAUTHLeave').prop('disabled', true);
$('#DummyBTNRJTLeave').prop('disabled', true);
}
}
}
}
check the specifyed checkbox in gridview
function Check_Click(objRef) {
//Get the Row based on checkbox
var row = objRef.parentNode.parentNode;
if (objRef.checked) {
//If checked change color to Aqua
row.style.backgroundColor = "#e3f1ff";
}
else {
//If not checked change back to original color
if (row.rowIndex % 2 == 0) {
//Alternating Row Color
row.style.backgroundColor = "rgba(0,0,0,.05)";
}
else {
row.style.backgroundColor = "white";
}
}
//Get the reference of GridView
var GridView = row.parentNode;
//Get all input elements in Gridview
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
//The First element is the Header Checkbox
var headerCheckBox = inputList[0];
//Based on all or none checkboxes
//are checked check/uncheck Header Checkbox
var checked = true;
if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
if (!inputList[i].checked) {
checked = false;
break;
}
}
}
headerCheckBox.checked = checked;
}

Dynamically added controls missing during GridView RowUpdating

I have a GridView with a TemplateField column that I put PlaceHolder controls in. During the DataBound event for the GridView I dynamically add a few CheckBoxes to the PlaceHolder. That works fine and displays as expected.
My problem is that during the RowUpdating event the PlaceHolder contains no controls; my CheckBoxes are missing. I also noticed that they're missing during the RowEditing event.
I want to be able to get the values of the CheckBoxes during the RowUpdating event so I can save them to the database.
Here's some example code. I've trimmed out a lot to reduce size, but if you want to see specifics just ask and I'll be happy to add more.
HTML:
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False"
ondatabound="gridView_DataBound" onrowupdating="gridView_RowUpdating"
onrowediting="gridView_RowEditing" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Countries">
<ItemTemplate>
<asp:PlaceHolder ID="countriesPlaceHolder" runat="server"></asp:PlaceHolder>
</ItemTemplate>
<EditItemTemplate>
<asp:PlaceHolder ID="countriesPlaceHolder" runat="server"></asp:PlaceHolder>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="editButton" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="updateButton" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
// This method works fine, no obvious problems here.
protected void gridView_DataBound(object sender, EventArgs e)
{
// Loop through the Holidays that are bound to the GridView
var holidays = (IEnumerable<Holiday>)gridView.DataSource;
for (int i = 0; i < holidays.Count(); i++)
{
// Get the row the Holiday is bound to
GridViewRow row = gridView.Rows[i];
// Get the PlaceHolder control
var placeHolder = (PlaceHolder)row.FindControl("countriesPlaceHolder");
// Create a CheckBox for each country and add it to the PlaceHolder
foreach (Country country in this.Countries)
{
bool isChecked = holidays.ElementAt(i).Countries.Any(item => item.ID == country.ID);
var countryCheckBox = new CheckBox
{
Checked = isChecked,
ID = country.Abbreviation + "CheckBox",
Text = country.Abbreviation
};
placeHolder.Controls.Add(countryCheckBox);
}
}
}
protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
// EXAMPLE: I'm expecting checkBoxControls to contain my CheckBoxes, but it's empty.
var checkBoxControls = gridView.Rows[e.NewEditIndex].FindControl("countriesPlaceHolder").Controls;
gridView.EditIndex = e.NewEditIndex;
BindData();
}
protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// EXAMPLE: I'm expecting checkBoxControls to contain my CheckBoxes, but it's empty.
var checkBoxControls = ((PlaceHolder)gridView.Rows[e.RowIndex].FindControl("countriesPlaceHolder")).Controls;
// This is where I'd grab the values from the controls, create an entity, and save the entity to the database.
gridView.EditIndex = -1;
BindData();
}
This is the article that I followed for my data binding approach: http://www.aarongoldenthal.com/post/2009/04/19/Manually-Databinding-a-GridView.aspx
You need to call your BindData() method on page load.
"Dynamic controls or columns need to be recreated on every page load, because of the way that controls work. Dynamic controls do not get retained so you have to reload them on every page postback; however, viewstate will be retained for these controls."
See Cells in gridview lose controls on RowUpdating event
Also in the article you linked, there is an ItemTemplate and an EditItemTemplace because they have different displays, i.e. read only and editable. Yours are the same so I think you could simplify your design:
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" ondatabound="gridView_DataBound">
<Columns>
<asp:TemplateField HeaderText="Countries">
<ItemTemplate>
<asp:PlaceHolder ID="countriesPlaceHolder" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="editButton" runat="server" Text="Edit" onclick="editButton_Click" ></asp:LinkButton>
<asp:LinkButton ID="updateButton" runat="server" Text="Update" onclick="updateButton_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
protected void gridView_DataBound(object sender, EventArgs e)
{
// Loop through the Holidays that are bound to the GridView
var holidays = (IEnumerable<Holiday>)gridView.DataSource;
for (int i = 0; i < holidays.Count(); i++)
{
// Get the row the Holiday is bound to
GridViewRow row = gridView.Rows[i];
// Get the PlaceHolder control
var placeHolder = (PlaceHolder) row.FindControl("countriesPlaceHolder");
var countryCheckBox = new CheckBox
{
Checked = true,
ID = "auCheckBox",
Text = "Aus",
Enabled = false
};
placeHolder.Controls.Add(countryCheckBox);
var editButton = (LinkButton)row.FindControl("editButton");
editButton.CommandArgument = i.ToString();
var updateButton = (LinkButton)row.FindControl("updateButton");
updateButton.CommandArgument = i.ToString();
updateButton.Visible = false;
}
}
protected void editButton_Click(object sender, EventArgs e)
{
LinkButton editButton = (LinkButton) sender;
int index = Convert.ToInt32(editButton.CommandArgument);
GridViewRow row = gridView.Rows[index];
// Get the PlaceHolder control
LinkButton updateButton = (LinkButton)row.FindControl("updateButton");
updateButton.Visible = true;
editButton.Visible = false;
CheckBox checkbox = (CheckBox)row.FindControl("auCheckBox");
if (checkbox != null)
{
checkbox.Enabled = true;
// Get value and update
}
}
protected void updateButton_Click(object sender, EventArgs e)
{
LinkButton updateButton = (LinkButton)sender;
int index = Convert.ToInt32(updateButton.CommandArgument);
GridViewRow row = gridView.Rows[index];
// Get the PlaceHolder control
LinkButton editButton = (LinkButton)row.FindControl("updateButton");
editButton.Visible = true;
updateButton.Visible = false;
CheckBox checkbox = (CheckBox)row.FindControl("auCheckBox");
if (checkbox != null)
{
// Get value and update
checkbox.Enabled = false;
}
}
If you want to be it enabled from the get go, just remove the enabled checks and you can delete your edit button.
Hope that helps.

Categories