Using grid view binding it from code behind:
I want to bind a particular column data into a hyper link so when it clicked it should do a download.
How to do that ?
Below is my code :
for (int i = 0; i <= tbl.Columns.Count - 1; i++)
{
Telerik.Web.UI.GridBoundColumn boundfield = new Telerik.Web.UI.GridBoundColumn();
if (tbl.Columns[i].ColumnName.ToString() == "Row")
{
LinkButton lkbtn = new LinkButton();
lkbtn.CommandName = i;
lkbtn.CommandArgument = "dwnld";
lkbtn.Font.Underline = true;
lkbtn.Text = tbl.Columns(i).ColumnName.ToString();
boundfield.DataField = tbl.Columns(i).ColumnName.ToString()
boundfield.HeaderText = tbl.Columns(i).ColumnName.ToString();
GridView2.MasterTableView.Columns.Add(boundfield);
}
}
Why not use grid template column with link button.
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="btnDownload" OnClick="btnDownload_Click" runat="server">Download Something</asp:LinkButton>
</ItemTemplate>
</telerik:GridTemplateColumn>
protected void btnDownload_Click(object sender, EventArgs e)
{
LinkButton lbBtn = sender as LinkButton;
GridDataItem item = (GridDataItem)(sender as LinkButton).NamingContainer;
// Use item to get other details
...
...
}
I have tried to build one gridview with dynamic columns based the data source using the template fields in asp.net through code behind.
For that, to implement we have developed one class DynamicTemplate which implements the ITemplate interface. In that template fields i have inserted the LinkButton in each cell and when i click that cell link button i need to show the one Popup with selected cell value.
For Detailed Sample Please download from this link
For that I have created one Default.asxp page and wrote the following.
public partial class Default : System.Web.UI.Page
{
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GenateGridView();
}
private void GenateGridView()
{
TemplateField tempField;
DynamicTemplate dynTempItem;
LinkButton lnkButton;
Label label;
GridView gvDynamicArticle = new GridView();
gvDynamicArticle.Width = Unit.Pixel(500);
gvDynamicArticle.BorderWidth = Unit.Pixel(0);
gvDynamicArticle.Caption = "<div>Default Grid</div>";
gvDynamicArticle.AutoGenerateColumns = false;
DataTable data = getBindingData();
for (int i = 0; i < data.Columns.Count; i++)
{
tempField = new TemplateField();
dynTempItem = new DynamicTemplate(ListItemType.AlternatingItem);
lnkButton = new LinkButton();
lnkButton.ID = string.Format("lnkButton{0}", i);
lnkButton.Visible = true;
string ColumnValue = data.Columns[i].ColumnName;
tempField.HeaderText = ColumnValue;
if (ColumnValue == "EmpName")
{
label = new Label();
label.ID = string.Format("Label{0}", i);
dynTempItem.AddControl(label, "Text", ColumnValue);
label.Width = 100;
}
else
{
dynTempItem.AddControl(lnkButton, "Text", ColumnValue);
lnkButton.Click += lnkButton_Click;
}
tempField.ItemTemplate = dynTempItem;
gvDynamicArticle.Columns.Add(tempField);
//////grdUserPivotDateTwo.Columns.Add(tempField);
}
gvDynamicArticle.DataSource = data;
gvDynamicArticle.DataBind();
divContainer.Controls.Add(gvDynamicArticle);
}
void lnkButton_Click(object sender, EventArgs e)
{
// showing cell values in popUp here..
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('cell clicked')");
}
private DataTable getBindingData()
{
dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpName"));
dt.Columns.Add(new DataColumn("Monday"));
dt.Columns.Add(new DataColumn("TuesDay"));
dt.Columns.Add(new DataColumn("WednesDay"));
dt.Columns.Add(new DataColumn("ThursDay"));
dt.Rows.Add("EmpOne", "p", "p", "p", "a");
dt.Rows.Add("EmpTwo", "p", "a", "p", "p");
dt.Rows.Add("EmpThree", "p", "p", "p", "a");
dt.Rows.Add("EmpFour", "p", "a", "p", "p");
dt.Rows.Add("EmpFive", "p", "p", "p", "a");
dt.Rows.Add("EmpSix", "a", "p", "p", "p");
return dt;
}
}
and corresponding DynamicTemplate class is
public class DynamicTemplate : System.Web.UI.ITemplate
{
System.Web.UI.WebControls.ListItemType templateType;
System.Collections.Hashtable htControls = new System.Collections.Hashtable();
System.Collections.Hashtable htBindPropertiesNames = new System.Collections.Hashtable();
System.Collections.Hashtable htBindExpression = new System.Collections.Hashtable();
public DynamicTemplate(System.Web.UI.WebControls.ListItemType type)
{
templateType = type;
}
public void AddControl(WebControl wbControl, String BindPropertyName, String BindExpression)
{
htControls.Add(htControls.Count, wbControl);
htBindPropertiesNames.Add(htBindPropertiesNames.Count, BindPropertyName);
htBindExpression.Add(htBindExpression.Count, BindExpression);
}
public void InstantiateIn(System.Web.UI.Control container)
{
PlaceHolder ph = new PlaceHolder();
for (int i = 0; i < htControls.Count; i++)
{
//clone control
Control cntrl = CloneControl((Control)htControls[i]);
switch (templateType)
{
case ListItemType.Header:
break;
case ListItemType.Item:
ph.Controls.Add(cntrl);
break;
case ListItemType.AlternatingItem:
ph.Controls.Add(cntrl);
ph.DataBinding += new EventHandler(Item_DataBinding);
break;
case ListItemType.Footer:
break;
}
}
ph.DataBinding += new EventHandler(Item_DataBinding);
container.Controls.Add(ph);
}
public void Item_DataBinding(object sender, System.EventArgs e)
{
PlaceHolder ph = (PlaceHolder)sender;
GridViewRow ri = (GridViewRow)ph.NamingContainer;
for (int i = 0; i < htControls.Count; i++)
{
if (htBindPropertiesNames[i].ToString().Length > 0)
{
Control tmpCtrl = (Control)htControls[i];
String item1Value = (String)DataBinder.Eval(ri.DataItem, htBindExpression[i].ToString());
Control ctrl = ph.FindControl(tmpCtrl.ID);
Type t = ctrl.GetType();
System.Reflection.PropertyInfo pi = t.GetProperty(htBindPropertiesNames[i].ToString());
pi.SetValue(ctrl, item1Value.ToString(), null);
}
}
}
private Control CloneControl(System.Web.UI.Control src_ctl)
{
Type t = src_ctl.GetType();
Object obj = Activator.CreateInstance(t);
Control dst_ctl = (Control)obj;
PropertyDescriptorCollection src_pdc = TypeDescriptor.GetProperties(src_ctl);
PropertyDescriptorCollection dst_pdc = TypeDescriptor.GetProperties(dst_ctl);
for (int i = 0; i < src_pdc.Count; i++)
{
if (src_pdc[i].Attributes.Contains(DesignerSerializationVisibilityAttribute.Content))
{
object collection_val = src_pdc[i].GetValue(src_ctl);
if ((collection_val is IList) == true)
{
foreach (object child in (IList)collection_val)
{
Control new_child = CloneControl(child as Control);
object dst_collection_val = dst_pdc[i].GetValue(dst_ctl);
((IList)dst_collection_val).Add(new_child);
}
}
}
else
{
dst_pdc[src_pdc[i].Name].SetValue(dst_ctl, src_pdc[i].GetValue(src_ctl));
}
}
return dst_ctl;
}
}
Here the Data showing in gridview is fine. Here the Issues are when i click on the linkButton the page reloads and no grid is displaying after the postback.
second issue is, for LinkButton the Click Event is not firing.
Please provide me the help full information/Sample to show the modal window when we click on the linkButton of the gridview.
You need to use ajax model popup expender.
Design a panel with your fields and use the model popup expender to display that popup
This link has the sample of it
www.asp.net/ajaxlibrary/ajaxcontroltoolkitsamplesite/modalpopup/modalpopup.aspx
and in your link button click
you have to use show method to open popup
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GenateGridView();
}
this code is generating a gridview only if it is not a postback, but when you click a linkbutton a postpack occurs and that's the reason why gridview doesnt show again when you click on the linkbutton.
add the following code(additional else part included to your if code) to show gridview when you click lnkButton
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GenateGridView();
else
{
string ctrlName = Request.Params.Get("__EVENTTARGET").Trim();
if (!String.IsNullOrEmpty(ctrlName))
{
if (ctrlName.StartsWith("lnkButton"))
{
GenateGridView();
}
}
}
}
It will be a good choice to use CommandName property for the linkbutton on the Gridview and give it a specfic name and in the code file and exactly work with it in RowCommand event of your GridView as following in this example :
first here is the .aspx file :
<div>
<asp:GridView ID="GridViewStudents" runat="server" AutoGenerateColumns="False" OnRowCommand="GridViewStudents_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Stud_ID" Visible="False">
<ItemTemplate>
<asp:Label ID="LabelStudID" runat="server" Text='<%# Eval("Stud_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FKFather_ID" Visible="False">
<ItemTemplate>
<asp:Label ID="LabelFkFatherID" runat="server" Text='<%# Eval("Fk_Father_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="LabelStudName" runat="server" Text='<%# Eval("Stud_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Class Name">
<ItemTemplate>
<asp:Label ID="LabelRowlevelName" runat="server" Text='<%# Eval("Stud_Level_Row_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandArgument='<%# Eval("Stud_ID") %>' CommandName="Remove" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div style="direction: ltr">
<asp:Panel ID="Panel1" runat="server" Visible="false">
<asp:Label ID="Labelpopupmessage" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="Buttonaccept" runat="server" Text="نعم" OnClick="Buttonaccept_Click" />
<asp:Button ID="Buttoncancel" runat="server" Text="لا" OnClick="Buttoncancel_Click" />
</asp:Panel>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:ModalPopupExtender runat="server" ID="ModalPopupExtenderStudent" PopupControlID="ButtonSubmit" TargetControlID="HiddenField1" CancelControlID="Buttoncancel">
</asp:ModalPopupExtender>
</div>
and here is the code implementation of my illustration :
protected void GridViewStudents_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Remove")
{
// I stored the ID of the selected Student I want to delete in a viewstate.
ViewState.Add("DeletedStudDetailID",Convert.ToInt32(e.CommandArgument));
ModalpopupExtender.Show();
}
}
// Here in the accept delete button I used that code ..
protected void Buttonaccept_Click(object sender, EventArgs e)
{
try
{
if (ViewState["DeletedStudDetailID"] != null)
{
StudentDetail StudDet = Data.StudentDetails.Single(SD => SD.Fk_Stud_ID == Convert.ToInt32(ViewState["DeletedStudDetailID"]));
Data.StudentDetails.DeleteOnSubmit(StudDet);
Student Stud = Data.Students.Single(S => S.Stud_ID == Convert.ToInt32(ViewState["DeletedStudDetailID"]));
Data.Students.DeleteOnSubmit(Stud);
Data.SubmitChanges();
}
this.ResultMessage = "Delete Done Sucessfully !!";
}
catch
{
this.ErrorMessage = "Delete operation disordered !!";
}
finally
{
ModalPopExtender.Hide();
}
}
I hope it helps in your issue and I wish you a happy day :) !!
First, your GridView will be created when calling GenateGridView method, so you have to call this method everytime you do post back, then your Page_Load should be
protected void Page_Load(object sender, EventArgs e)
{
GenateGridView();
}
Second, I would present you the other way to add LinkButton to GridView dynamically.
I modified your GenateGridView to just add only label into DynamicTemplate, also add this line gvDynamicArticle.RowDataBound += new GridViewRowEventHandler(gvDynamicArticle_RowDataBound); to handle adding LinkButton.
private void GenateGridView()
{
TemplateField tempField;
DynamicTemplate dynTempItem;
Label label;
GridView gvDynamicArticle = new GridView();
gvDynamicArticle.Width = Unit.Pixel(500);
gvDynamicArticle.BorderWidth = Unit.Pixel(0);
gvDynamicArticle.Caption = "<div>Default Grid</div>";
gvDynamicArticle.AutoGenerateColumns = false;
gvDynamicArticle.RowDataBound += new GridViewRowEventHandler(gvDynamicArticle_RowDataBound);
DataTable data = getBindingData();
for (int i = 0; i < data.Columns.Count; i++)
{
tempField = new TemplateField();
dynTempItem = new DynamicTemplate(ListItemType.AlternatingItem);
string ColumnValue = data.Columns[i].ColumnName;
tempField.HeaderText = ColumnValue;
label = new Label();
label.ID = string.Format("Label{0}", i);
dynTempItem.AddControl(label, "Text", ColumnValue);
label.Width = 100;
tempField.ItemTemplate = dynTempItem;
gvDynamicArticle.Columns.Add(tempField);
}
gvDynamicArticle.DataSource = data;
gvDynamicArticle.DataBind();
divContainer.Controls.Add(gvDynamicArticle);
}
I implement like this in RowDataBound event handler of the GridView to add LinkButton and hide the Label which we added it before:
protected void gvDynamicArticle_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int j = 1; j < e.Row.Cells.Count; j++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkButton = new LinkButton();
lnkButton.ID = string.Format("lnkButton{0}{1}", e.Row.DataItemIndex, j);
lnkButton.Click += new EventHandler(lnkButton_Click);
Label tempLabel = e.Row.FindControl("Label" + j) as Label;
lnkButton.Text = tempLabel.Text;
lnkButton.CommandArgument = tempLabel.Text;
tempLabel.Visible = false;
e.Row.Cells[j].Controls.Add(lnkButton);
}
}
}
You can also set any value to CommandArgument of LinkButton, then you can show it in the alert.
Final, seems you want to show some value in the alert, you may code like this
public void lnkButton_Click(object sender, EventArgs e)
{
// showing cell values in popUp here..
LinkButton lnk = (LinkButton)sender;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('cell clicked, value " + lnk.CommandArgument + "')", true);
}
I want to bind my grid column header names by retrieving data from a table. This table has two fields, DomainID and DomainName, I want to display the DomainNames as Column header of the Grid.
Actually I am creating employee grid view. I want all the domain names of employee to be displayed as a header and i have to check the corresponding domain in Checkbox.
Please Give me some ideas.
Thanks in advance.
From what i understood....
Make a grid view
Create two columns:
a. TextBoxColumn
b. CheckBoxColumn
Set your column header using .HeaderText property
Add the columns to your data grid view
Query your database and get the data_table from it
using dgv.DataSource = data_table bind your data to the table
OR
Make a for loop for all rows in the data_table and add each row explicitly
For getting your checkboxes to work, handle the cellContentClick event of the data grid view and perform the necessary updates in your database.....
Hope it helps....
You could load the headers into a DataTable and then create them dynamically with a custom TemplateField.
Here's the aspx part:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" DataKeyNames="EmployeeID" runat="server" >
<SelectedRowStyle BackColor="Aqua" />
<Columns>
<asp:TemplateField HeaderText="Employee" SortExpression="Employee">
<ItemTemplate>
<asp:HiddenField ID="HiddenEmpID" Value='<%# Bind("EmployeeID") %>' runat="server" />
<asp:label runat="server" ID="LblEmployee" Text='<%# Bind("EmployeeName") %>'></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="BtnSave" Text="Save" runat="server" onclick="BtnSave_Click" />
Here's a complete sample:
public partial class GridTest : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
CreateGridColumns();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) BindGrid();
}
private void CreateGridColumns()
{
var tblDomain = GetDomains();
// Create dynamic TemplateFields
foreach (DataRow row in tblDomain.Rows)
{
String domainName = row.Field<String>("DomainName");
TemplateField field = new TemplateField();
//Initalize the DataField value.
field.ItemTemplate = new GridViewCheckBoxTemplate(ListItemType.Item, domaninName);
field.HeaderText = domainName;
//Add the newly created field to the GridView.
GridView1.Columns.Add(field);
}
}
private DataTable GetDomains()
{
var tblDomain = new DataTable();
tblDomain.Columns.Add("DomainID", typeof(int));
tblDomain.Columns.Add("DomainName");
tblDomain.Rows.Add(1, "Google.com");
tblDomain.Rows.Add(2, "Yahoo.com");
tblDomain.Rows.Add(3, "Msn.com");
tblDomain.Rows.Add(4, "Youtube.com");
tblDomain.Rows.Add(5, "Myspace.com");
tblDomain.Rows.Add(6, "Facebook.com");
tblDomain.Rows.Add(7, "Wikipedia.org");
return tblDomain;
}
private void BindGrid()
{
var tblDomain = GetDomains(); // load domains from database or wherever
var tblData = new DataTable();// load sample data
tblData.Columns.Add("EmployeeID", typeof(int));
tblData.Columns.Add("EmployeeName");
//add domains as DataTable-Columns
foreach (DataRow row in tblDomain.Rows)
{
String domaninName = row.Field<String>("DomainName");
//Add column from domain-name
tblData.Columns.Add(domaninName, typeof(bool)); //CheckBox-Checked is a boolean
}
//get some Employees and random checked state
var rnd = new Random();
var empRow = tblData.NewRow();
empRow["EmployeeID"] = 1;
empRow["EmployeeName"] = "Jon";
foreach (DataRow dom in tblDomain.Rows)
{
empRow[dom.Field<String>("DomainName")] = rnd.Next(0, 2) == 0;
}
tblData.Rows.Add(empRow);
empRow = tblData.NewRow();
empRow["EmployeeID"] = 2;
empRow["EmployeeName"] = "Eric";
foreach (DataRow dom in tblDomain.Rows)
{
empRow[dom.Field<String>("DomainName")] = rnd.Next(0, 2) == 0;
}
tblData.Rows.Add(empRow);
empRow = tblData.NewRow();
empRow["EmployeeID"] = 3;
empRow["EmployeeName"] = "Alain";
foreach (DataRow dom in tblDomain.Rows)
{
empRow[dom.Field<String>("DomainName")] = rnd.Next(0, 2) == 0;
}
tblData.Rows.Add(empRow);
GridView1.DataSource = tblData;
GridView1.DataBind();
}
// show how to retrieve all checkbox values and the according EmployeeID
protected void BtnSave_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0) return;
var checkBoxColumns = GridView1.Columns.Cast<DataControlField>()
.Select((bf,index) => new{Field=bf, Index=index})
.Where(f => f.Field.GetType() == typeof(TemplateField) && ((TemplateField)f.Field).ItemTemplate.GetType() == typeof(GridViewCheckBoxTemplate))
.ToArray();
foreach (GridViewRow row in GridView1.Rows)
{
int EmployeeID = int.Parse(((HiddenField)row.FindControl("HiddenEmpID")).Value);
foreach (var f in checkBoxColumns)
{
String domain = f.Field.HeaderText;
bool isChecked = row.Controls[f.Index].Controls.OfType<CheckBox>().First().Checked;
}
}
}
}
Here's the custom ITemplate:
public class GridViewCheckBoxTemplate : ITemplate
{
ListItemType _templateType;
string _columnName;
public GridViewCheckBoxTemplate(ListItemType type, string colname)
{
_templateType = type;
_columnName = colname;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_templateType)
{
case ListItemType.Header:
break;
case ListItemType.Item:
var chb1 = new CheckBox();
chb1.DataBinding += new EventHandler(CB_DataBinding);
container.Controls.Add(chb1);
break;
case ListItemType.EditItem:
//As, I am not using any EditItem, I didnot added any code here.
break;
case ListItemType.Footer:
break;
}
}
void CB_DataBinding(object sender, EventArgs e)
{
CheckBox chb = (CheckBox)sender;
GridViewRow container = (GridViewRow)chb.NamingContainer;
object dataValue = ((DataRowView)container.DataItem)[_columnName];
chb.Checked = dataValue != DBNull.Value && (bool)dataValue;
}
}
I already retrieved my database(DescriptionCode) on dropdownlist inside on a repeater.
Now, I'm trying to save/add/insert on my database the selected value of dropdownlist but i
failed.
Any assistance gratefully received. Thanks!
protected void GeneralRepeater_OnItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList myDDL = (DropDownList)e.Item.FindControl("GeneralDDL");
Diagnosis oDiagnosis = new Diagnosis();
PlanOfCare oPlanOfCare = new PlanOfCare();
DataView dv = new DataView(oDiagnosis.GetDiagnosis());
myDDL.DataSource = PatientDiagnosis1;
myDDL.DataTextField = "DiagnosisCode";
myDDL.DataValueField = "DiagnosisCode";
myDDL.DataBind();
//PUT AN EMPTY FIELD FOR DROPDOWNLIST
ListItem LI = new ListItem("", "");
myDDL.Items.Insert(0, LI);
myDDL.SelectedValue = "0";
}
}
protected void cmdSave_Click(object sender, EventArgs e)
{
oPlanofCareSave.DiagnosesCode = //[1]this must the selected value of dropdownlist inside of repater
PlanSave(ooPlanofCareSave);
}
What about the cmdSave button, is that is inside the Repeater
You need to find out myDDL inside each row and for each of them you need to get this value.
foreach (RepeaterItem rptItem in RepeaterName.Rows)
{
DropDownList myDDL = (DropDownList)rptItem.FindControl("myDDL");
}
DropDownList myDDL = (DropDownList)GeneralRepeater.Items[indexvalue].FindControl("GeneralDDL");
oPlanofCareSave.DiagnosesCode = myDDL.SelectedValue;
it is easy to do it like that
I have a FormView that I user for updating a record. There is a link button that when fires should perforom the updating via BLL and DAL. I am not using built-in ODS and I will not condsider using it.
I have all my grids and formviews populated manuualy by calling methods that fetch the data from the database.
For instance my details view is populated like this:
protected void DlMembers_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.ToString() == "Select")
{
DlMembers.Visible = false;
lblError.Text = string.Empty;
lblError.Visible = false;
fvMemberDetail.Visible = true;
fvMemberDetail.ChangeMode(FormViewMode.Edit);
MemberBLL getMemberInfo = new MemberBLL();
int Ident = Convert.ToInt32(e.CommandArgument.ToString());
fvMemberDetail.DataSource = getMemberInfo.GetMemberByIdent(Ident);
fvMemberDetail.DataBind();
}
if (e.CommandName.ToString() == "DeleteSelected")
{
DlMembers.Visible = true;
lblError.Text = string.Empty;
lblError.Visible = false;
fvMemberDetail.Visible = false;
fvMemberDetail.ChangeMode(FormViewMode.ReadOnly);
}
What I want to do if to capature my linkbutton on click event and do this (except that the runtime never reaches this method):
protected void MemberInfoUpdating(object sender, EventArgs e)
{
TextBox id = (TextBox)fvMemberDetail.FindControl("txtIdent");
if (id.Text != string.Empty || id.Text != "")
{
TextBox txtFN = (TextBox)fvMemberDetail.FindControl("txtFN");
TextBox txtLN = (TextBox)fvMemberDetail.FindControl("txtLN");
DropDownList ddlAddress = (DropDownList)fvMemberDetail.FindControl("ddlAddress");
TextBox txtEmail = (TextBox)fvMemberDetail.FindControl("txtEmail");
TextBox txtHPhone = (TextBox)fvMemberDetail.FindControl("txtHPhone");
TextBox txtWPhone = (TextBox)fvMemberDetail.FindControl("txtWPhone");
TextBox txtMPhone = (TextBox)fvMemberDetail.FindControl("txtMPhone");
DropDownList ddlPos = (DropDownList)fvMemberDetail.FindControl("ddlPos");
DropDownList ddlIsAdmin = (DropDownList)fvMemberDetail.FindControl("ddlIsAdmin");
bool blIsAdmin = false;
if (ddlIsAdmin.SelectedValue == "True") blIsAdmin = true;
TextBox txtComments = (TextBox)fvMemberDetail.FindControl("txtComments");
MemberBLL updateMemberInfo = new MemberBLL();
bool UpdateOK = updateMemberInfo.UpdateMemberByIdent(
txtFN.Text,
txtLN.Text,
ddlAddress.SelectedValue,
txtEmail.Text,
txtHPhone.Text,
txtWPhone.Text,
txtMPhone.Text,
blIsAdmin,
txtComments.Text,
Convert.ToInt32(ddlPos.SelectedValue),
Convert.ToInt32(id.Text));
}
else
{
//Display error - no user id cannot update record
}
}
The linkbutton looks like this:
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
OnClick="MemberInfoUpdating" Text="Update" />
Where is this LinkButton? If it's inside a FormView template, then you'll likely need to use something like this instead:
<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
Then handle the "Update" command in DlMembers_ItemCommand.
Alternatively, attach your code to the OnItemUpdating event of the FormView rather than some extra event you don't need:
<asp:FormView ID="fvMemberDetail" runat="server" OnItemUpdating="MemberInfoUpdating">