Gridview column linkbutton change the value according to data - c#

I am trying to complete a simple task;
a gridview column gets an integer data if integer is equals to zero print "active" and set the commandname of the linkbutton to active else set linkbutton text and command value to inactive.
here is what I have so far
<Columns>
<asp:BoundField HeaderText = "User Name" DataField="UserName"
HeaderStyle-Width="16%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText = "Role" DataField="RoleNAME"
HeaderStyle-Width="14%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText = "Group" DataField="GroupName"
HeaderStyle-Width="12%" ItemStyle-HorizontalAlign="Center" />
<asp:ButtonField ButtonType="link" CommandName = "Active"
HeaderText="Status" Text="Active"
HeaderStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:ButtonField ButtonType="link" CommandName = "Edit"
HeaderText="" Text="Edit/View"
HeaderStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
</Columns>
codebehind
protected void grdMyQueue_RowdataBound(object sender, GridViewRowEventArgs e)
{
// In template column,
if (e.Row.RowType == DataControlRowType.DataRow)
{
var obj = (User)e.Row.DataItem;
if (obj.isDisabled == 0)
{
LinkButton linkButton = new LinkButton();
linkButton.Text = "Active";
linkButton.Enabled = true;
linkButton.CommandName = "Active";
//linkButton.CommandArgument = e.Row. //this might be causing the problem
e.Row.Cells[3].Controls.Clear();
e.Row.Cells[3].Controls.Add(linkButton);
}
else
{
LinkButton linkButton = new LinkButton();
linkButton.Text = "InActive";
linkButton.Enabled = true;
linkButton.CommandName = "InActive";
e.Row.Cells[3].Controls.Add(linkButton);
}
}
}
However when I Click the active linkbutton on the cell I get an error at onrowcommand function
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument); // this is the error line
GridViewRow gvRow = userManager.Rows[index];
TableCell usrName = gvRow.Cells[0];
string userName = usrName.Text;
....
How can I manage to change LinkButton text and CommandName depending on that integer data?
P.S I tried Eval but I couldnt figure out whats going on....

I would simply return the data from your data source, as intended. Then I would use an Update Panel to avoid the Postback that will occur from your Link Button. As for the text modifying I would use Javascript, so Javascript will read the page on the client. So when your data source returns:
One
One
Two
One
The Javascript can analyze those rows, then modify the internal cell within the Grid quite painless.
An example of Javascript:
$(".Activator").each(function () {
if ($(this).prev().find(':checkbox').prop('checked')) {
$(this).text("Deactivate");
}
});
This example will validate if a checkbox is checked, if it is modify the internal text to the class .Activator.
Then the internal item in the Grid for code on front-end would look like:
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<asp:CheckBox
ID="CustomerCheck" runat="server"
Checked='<%# Eval("Active") %>'
Enabled="false" />
<asp:LinkButton
ID="lbCustomerActive" runat="server"
Text="Activate"
CommandName="OnActivate"
ToolTip='<%# "Activate or Deactivate Course: " + Eval("CourseID") %>'
OnClick="CustomerCheck_Click"
CssClass="Activator">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
As you can see, as the internal data is changed the Javascript will automatically adjust the value for me every time the Data Source is binded again. Which the CustomerCheck_Click would execute the code on the server, which will read the Command Argument which contains the Row Id. Which will Update that particular record without a problem and rebind the Grid. (Partially lied, I'm parsing the Tooltip).
You would instantiate on server side like:
((LinkButton)sender).ToolTip;

Related

C# asp.net Gridview navigate to url in gridview row

I'm currently working on an in house project, I've created a GridView connected to a SQL table which looks like this:
GridView1
I created the view content buttons using the following code:
<Columns>
<asp:ButtonField ButtonType="Button" Text="View Content" CommandName="Select" />
<asp:BoundField DataField="ContentID" HeaderText="ContentID" InsertVisible="False" ReadOnly="True" SortExpression="ContentID" />
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="URL" HeaderText="URL" Visible="true" SortExpression="URL" />
</Columns>
But this is where I am now stuck. I would like to click on the view content button and have it navigate to the URL on the selected row.
The URL comes from the SQL Table and there is a string so I'd imagine, it would need to be converted first but I could be wrong.
I started to put my code in the following:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewCommandEventArgs x = (GridViewCommandEventArgs)e;
if (x.CommandName == "Select")
{
GridViewRow row = GridView1.SelectedRow;
}
}
Add your code in GridView1_RowCommand event of gridview:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow row = (GridViewRow)(((BoundField)e.CommandSource).NamingContainer);
int index = row.RowIndex;
string url = (GridView1.Rows[index].FindControl("URL") as BoundField).Text;
Response.Redirect(url); // url can be from your sql table
}
}
Note: Don't forget to add OnRowCommand event in GrindView <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" >
GridViewRow row = (GridViewRow)(((BoundField)e.CommandSource).NamingContainer);
int index = row.RowIndex;
string url = (GridView1.Rows[index].FindControl("URL") as BoundField).Text;
Response.Redirect(url); // url can be from your sql table
So i made the change. Unfortunately, BoundField does not contain a definition for NamingContainer.
Also, GridView1.Rows[index].FindControl("URL") as BoundField fails due to the following error, cannot convert type 'system.web.ui.control' to 'system.web.ui.webcontrols.boundfield' via a reference conversion, boxing conversion, unboxing conversion or null type conversion.

OnCheckChanged Event of asp:CheckBox Not Firing When Unchecked From Within Nested GridView

I have conducted a lot of searches which included checkchanged or OncheckChanged keyword:
ASP.NET CheckBox does not fire CheckedChanged event when unchecking
https://forums.asp.net/t/1311576.aspx?Checkbox+not+firing+when+unchecking+using+OnCheckedChanged
OnCheckedChanged event not firing
OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked
But this just doesn't seem to be working although I applied all suggestion from the links given above
I am tring to fire a CheckChanged Event From nested gridview, whose DataSource gets binded in parent grid's OnRowDataBound event.
My aspx markup
<asp:GridView ID="gvDocSchedule" runat="server" AutoGenerateColumns="false" CssClass="table table-striped color-black"
OnRowDataBound="gvDocSchedule_RowDataBound" DataKeyNames="UserID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor:pointer" src="UserPanel/images/Plus12.png" />
<asp:Panel ID="PanelSchedule" runat="server" Style="display:none">
<asp:GridView ID="gvScheduleDayNTime" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField ="ScheduleDay" HeaderText="CheckInTime1"/>
<asp:BoundField ItemStyle-Width="150px" DataField ="CheckInTime1" HeaderText="CheckInTime1"/>
<asp:BoundField ItemStyle-Width="150px" DataField ="CheckOutTime1" HeaderText="CheckOutTime1"/>
<asp:BoundField ItemStyle-Width="150px" DataField ="CheckInTime2" HeaderText="CheckInTime2"/>
<asp:BoundField ItemStyle-Width="150px" DataField ="CheckOutTime2" HeaderText="CheckOutTime2"/>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnForChkBox" runat="server" value="No" />
<asp:CheckBox ID="CBoxAvailabilityTime1" ViewStateMode="Enabled" Checked="false" Enabled="true" EnableViewState="true" runat="server" Text="See Free TimeSlots For Booking In Time1" AutoPostBack="true" OnCheckedChanged="CBoxAvailabilityTime1_CheckedChanged" />
<br />
<asp:CheckBox ID="CBoxAvailabilityTime2" runat="server" Text="See Free TimeSlots For Booking In Time1" AutoPostBack="true" OnCheckedChanged="CBoxAvailabilityTime2_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ControlStyle-Font-Bold="true" DataField="UserFirstName" HeaderText="Doctor's First Name" />
<asp:BoundField DataField="UserLastName" HeaderText="Doctor's Last Name" />
<asp:BoundField DataField="SpecializationName" HeaderText="Doctor's Specialization" />
<asp:BoundField DataField="HospitalName" HeaderText="Hospital" />
</Columns>
</asp:GridView>
I was trying it with CBoxAvailabilityTime1CheckBox. As you guys can see ViewStateMode and EnableViewState has been taken care of not only in server tag but also in content page's tag, also these properties are true for master page
Parent Grid gets binded when user press a button and nested grid gets binded in parent's OnRowDataBound event
Here is My aspx.cs code
protected void CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvParentRow in gvDocSchedule.Rows)
{
if (gvParentRow.RowType == DataControlRowType.DataRow)
{
GridView gvDocSchedulesGetChildGrid = (GridView)gvParentRow.FindControl("gvScheduleDayNTime");
if (gvDocSchedulesGetChildGrid != null)
{
foreach (GridViewRow gvChildRow in gvDocSchedulesGetChildGrid.Rows)
{
CheckBox CBoxAvailabilityTime1 = (CheckBox)gvChildRow.FindControl("CBoxAvailabilityTime1");
CheckBox CBoxAvailabilityTime2 = (CheckBox)gvChildRow.FindControl("CBoxAvailabilityTime2");
if (((CheckBox)CBoxAvailabilityTime1).Checked)
{
CBoxAvailabilityTime2.Enabled = false;
}
if (!((CheckBox)CBoxAvailabilityTime1).Checked)
{
CBoxAvailabilityTime2.Enabled = true;
}
}
}
}
}
}
With this setup CheckChanged Event Fires on Checking.
On Checking it hits Page_ Load skips if(!IsPostBack) (as AutoPostBack=true) and then control is directly transferred to
CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e) event handler function
but on the other hand it doesn't fire on unchecking it postsbacks goes to Page load again Skips if(!IsPostBack) and does nothing instead of calling
CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)
"Note:-" Page_Load is not involved. I can't bind parent grid in page_load,
!IsPostBack because I dont need to bind it at first time when page gets loaded rather its binding happens inside a button click event, if a button is clicked only then parent grid must get binded.
Update 1:- This is how I bind data to parent grid
I am calling this function in a button click event
protected void BindDataToGridViewDocInArea()
{
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
SqlCommand cmdFillGridDocInArea = new SqlCommand("some query with parameter here", con)
cmdFillGridDocInArea.Parameters.AddWithValue("#AID", ddlAskArea.SelectedValue);
SqlDataAdapter sdaFillGridDocInArea = new SqlDataAdapter(cmdFillGridDocInArea);
DataTable dtFillGridDocInArea = new DataTable();
sdaFillGridDocInArea.Fill(dtFillGridDocInArea);
if (dtFillGridDocInArea.Rows.Count > 0)
{
gvDocSchedule.DataSource = dtFillGridDocInArea;
gvDocSchedule.DataBind();
}
else
{
lblError.Text = string.Empty;
lblError.Text = "No Record Exists Against Requst Specified";
}
con.Dispose();
}
}
Tested a stripped down version of you snippet. It seems to be working. When you check CBoxAvailabilityTime1, it disables CBoxAvailabilityTime2. When you uncheck CBoxAvailabilityTime1 the other one is enabled again.
However this is only when the DataBinding of gvDocSchedule is inside an IsPostBack check. When it's not the checkboxes will always go to their default unchecked state after PostBack.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//this works
gvDocSchedule.DataSource = LoadFromDB();
gvDocSchedule.DataBind();
}
//this does not
gvDocSchedule.DataSource = LoadFromDB();
gvDocSchedule.DataBind();
}
Or in your case something like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDataToGridViewDocInArea();
}
}

how to get value of a template field label of a gridview, getting error Object reference not set to an instance of an object

While getting the value I'm getting Object reference not set to an instance of an object. how to get labeled label value in code behind. How to get the value in custom event
<asp:GridView runat="server" ID="gridviewQuoteDetails" EmptyDataText="No records Found..." AutoGenerateEditButton="false" OnRowEditing="gridviewQuoteDetails_RowEditing" OnRowUpdating="gridviewQuoteDetails_RowUpdating" DataKeyNames="id" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href='Quote.aspx?val=<%#Eval("id")%>'>
<asp:Label ID="lblid" runat="server" Text='<%#Eval ("id")%>'></asp:Label>
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Text="Update" runat="server" OnClick="OnUpdate" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind
protected void OnUpdate(object sender, EventArgs e)
{
string qouteid = ((Label)gridviewQuoteDetails.FindControl("lblid")).Text;
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string id = (row.Cells[0].Controls[0] as TextBox).Text;
string Description = (row.Cells[1].Controls[0] as TextBox).Text;
}
Gridview is a collection of rows right, but you are trying to search the control directly within the gridview thus ((LinkButton)gridviewQuoteDetails.FindControl("lbllnknm")) will be null and when you are trying to access the Text property of LinkButton you are getting NRE error.
You need to loop through the rows of your gridview control to access each linkbutton present in each row like this:-
foreach (GridViewRow row in gridviewQuoteDetails.Rows)
{
LinkButton lbllnknm= row.FindControl("lbllnknm") as LinkButton;
}
But ideally you would not be needing this. I guess you are trying to get the value in OnRowEditing event handler. If that is the case then you can fetch the value of linkbutton like this:-
protected void gridviewQuoteDetails_RowEditing(object sender, GridViewUpdateEventArgs e)
{
string qouteid = ((Label)gridviewQuoteDetails.Rows[e.NewEditIndex]
.FindControl("lbllnknm")).Text;
}
Update 2:
As per your comment you are trying to access the LinkButton text on click of LinkButton itself which means the event is raised by LinkButton itself. You can simply use the sender object like this:-
protected void OnUpdate(object sender, EventArgs e)
{
LinkButton LinkButton2 = sender as LinkButton;
GridViewRow row = LinkButton2.NamingContainer as GridViewRow; //Get the gridview row
Label lblid = row.FindControl("lblid") as Label;
string qouteid = lblid.Text;
}

Add email url to gridview row, and open email window on double click

I have a GridView that has a column that contains emails. I want the user to be able to double click the row and an email link is activated to open an outlook window for an email. I've got the double click part down, but I'm no sure how to get the email from the row to create the url. I'll paste the code I do have below.
<asp:GridView ID="gvAllDOL" runat="server" Visible="False" PageSize="25" AutoGenerateColumns="False" OnDataBound="gvAllDOL_DataBound" DataSourceID="odsDOAll" OnRowDataBound="gvAllDOL_RowDataBound" DataKeyNames="sintDistrictOfficeID" OnRowCommand="gvAllDOL_RowCommand" OnSelectedIndexChanged="gvAllDOL_SelectedIndexChanged">
<Columns>
<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" Visible="false" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("sintDistrictOfficeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sintDistrictOfficeID" HeaderText="id" SortExpression="sintDistrictOfficeID" />
<asp:BoundField DataField="vcharDOLOfficeName" HeaderText="DOL Office Name" SortExpression="vcharDOLOfficeName" />
<asp:BoundField DataField="vcharDOLCity" HeaderText="City" SortExpression="vcharDOLCity" />
<asp:BoundField DataField="vcharDOLState" HeaderText="State" SortExpression="vcharDOLState" />
<asp:BoundField DataField="intBatchCount" HeaderText="Number Batches" SortExpression="intBatchCount" />
<asp:BoundField DataField="intCaseCount" HeaderText="Number Cases" SortExpression="intCaseCount" />
<asp:BoundField DataField="intExamCount" HeaderText="Number Examiners" SortExpression="intExamCount" />
</Columns>
</asp:GridView>
protected void gvCE_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onMouseOver", "Highlight(this)");
e.Row.Attributes.Add("onMouseOut", "UnHighlight(this)");
// Get the LinkButton control in the second cell
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
// Get the javascript which is assigned to this LinkButton
string _jsDouble =
ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Add this JavaScript to the ondblclick Attribute of the row
e.Row.Attributes["ondblclick"] = _jsDouble;
}
}
protected void gvCE_RowCommand(object sender, GridViewCommandEventArgs e)
{
string email = ((Label)gvCE.Rows[0].Cells[1].FindControl("lblEmail")).Text; (this doesn't work)
GridView _gridView = (GridView)sender;
string _commandName = e.CommandName;
switch (_commandName)
{
case ("DoubleClick"):
Response.Redirect("<a href=mailto:" + email + ">");
break;
}
}
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in gvAllDOL.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl00");
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl01");
}
}
foreach (GridViewRow r in gvCE.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl00");
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl01");
}
}
base.Render(writer);
}
Convert your email field into template one. Add the following code and then try playing with it:
<asp:HyperLink ID="EmailLink" runat="server" Text='Email' NavigateUrl=
"mailto:" + '<%# Eval("yourBoundEmailFieldNameHere") %>'
</asp:HyperLink>
I would use Manul's suggestion on adding the email address through an item template
<asp:HyperLink ID="EmailLink" runat="server" Text='Email' NavigateUrl=
"mailto:" + '<%# Eval("yourBoundEmailFieldNameHere") %>'
</asp:HyperLink>
This way you can access the email address and people can see who they will be emailing.
then add this script to the page
$("table tr").dblclick(function () {
var mailto_link = $('a', $(this)).attr('href');
window = window.open(mailto_link, 'emailWindow');
if (window && window.open && !window.closed)
window.close();
});
then remove the code that is adding javascript functions to each row in the grid, because it is no longer needed.
here is a jsfiddle link http://jsfiddle.net/gorrilla/jEX7Y/
You can use command argument to pass the email address, greatly simplifying the logic necessary to retrieve the address.
Change the Command button column to an TemplateField and add a asp:button inside. Then add the attribute CommandArgument to the button.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="dblClick" runat="server" Text="dblClick"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="dblClick" />
</ItemTemplate>
</asp:TemplateField>
Then in the code behind
protected void gvCE_RowCommand(object sender, GridViewCommandEventArgs e)
{
string email = e.CommandArgument;

Gridview Button in TemplateField only fires on second click

I've done some searching prior to asking, and although this post is close, it doesn't work for my scenario.
What I find is that the "delete" button in my template field seems to fire, but the click event does not trigger. Yet the second time you click the button it works as expected.
So, breaking the code down, I am binding my data to a GridView, using a `SqlDataSource.
My page load event starts as follows:
if (!Page.IsPostBack)
{
externalUserDataSource.ConnectionString = "some connection string";
}
My data source is as follows:
<asp:SqlDataSource ID="externalUserDataSource" runat="server"
ConflictDetection="CompareAllValues" SelectCommand="uspGetExternalUsersByTeam"
SelectCommandType="StoredProcedure" ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:SessionParameter Name="TeamID" SessionField="TeamID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
And this is my GridView markup:
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataKeyNames="LoginID" DataSourceID="externalUserDataSource"
EnableModelValidation="True" OnRowDataBound="GridViewRowDataBound" TabIndex="3">
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="LightGoldenrodYellow" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<Columns>
<asp:BoundField DataField="RowID" HeaderText="Row ID" ReadOnly="True"
SortExpression="RowID" Visible="False" />
<asp:BoundField DataField="LoginID" HeaderText="Login ID" ReadOnly="True"
SortExpression="LoginID" Visible="False" />
<asp:BoundField DataField="EmailAddress" HeaderText="Email Address"
ItemStyle-VerticalAlign="Bottom" ReadOnly="True" SortExpression="AssociateName"/>
<asp:BoundField DataField="TeamID" HeaderText="Team ID" ReadOnly="True"
SortExpression="TeamID" Visible="False" />
<asp:CheckBoxField DataField="HasFIAccess"
HeaderText="Has Access to<br />Funding<br/>Illustrator"
ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Bottom"
ReadOnly="True"/>
<asp:CheckBoxField DataField="HasALTAccess"
HeaderText="Has Access to<br />Asset Liability<br/>Tracker"
ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Bottom"
ReadOnly="True"/>
<asp:CheckBoxField DataField="HasFIAAccess"
HeaderText="Has Access to<br />Funding<br/>Illustrator App"
ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Bottom"
ReadOnly="True"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" CssClass="additionsRow" ID="btnDeleteExternalUser" OnClick="DeleteExtUserButtonClick"
CausesValidation="False" Text="Delete"
CommandArgument='<%#Eval("TeamID") + "," + Eval("LoginID") + "," + Eval("EmailAddress") + "," + Eval("HasALTAccess")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
So, you can see that I am passing across some information in the button that is used in the event to ensure the correct data is deleted (which is why I cannot use the ButtonField, as suggested in the link above).
The last part to the puzzle is the GridView's databound event:
protected void GridViewRowDataBound(object sender,
GridViewRowEventArgs e)
{
// if rowtype is not data row...
if (e.Row.RowType != DataControlRowType.DataRow)
{
// exit with no further processing...
return;
}
// get the ID for the selected record...
var selectedId = DataBinder.Eval(e.Row.DataItem, "RowID").ToString();
// create unique row ID...
e.Row.ID = string.Format("ExtUserRow{0}", selectedId);
// find the button delete for the selected row...
var deleteButton = (Button)e.Row.FindControl("btnDeleteExtUser");
// get the email address for the selected record...
var selectedUser = DataBinder.Eval(e.Row.DataItem, "EmailAddress").ToString();
// define the message text...
var messageText = string.Format("OK to delete {0}?",
selectedUser.Replace("'", "\\'"));
// add attribute to row delete action...
this.AddConfirmMessage(deleteButton, messageText);
}
Where AddConfirmMessage simply assigns an onclick attribute to the control to ensure the user has to confirm the deletion.
Now, in every case the message pops up 'OK to delete abc#xyz.com?', but as stated earlier, the event assigned to the "delete" button does not trigger until the button is clicked a second time.
Strangely enough, I took this code from another page and modified accordingly, though it doesn't have this issue there:
protected void DeleteExtUserButtonClick(object sender,
EventArgs e)
{
// get the buton which was clicked...
var button = (Button)sender;
// break the delimited array up...
string[] argumentArray = button.CommandArgument.Split(',');
// store the items from the array...
string teamId = argumentArray[0];
string loginId = argumentArray[1];
string emailAddress = argumentArray[2];
string hasAltAccess = argumentArray[3];
using (var conn = new SqlConnection(Utils.GetConnectionString()))
{
// create database command...
using (var cmd = new SqlCommand())
{
// set the command type...
cmd.CommandType = CommandType.StoredProcedure;
// set the name of the stored procedure to call...
cmd.CommandText = "uspDeleteExternalUser";
// create and add parameter to the collection...
cmd.Parameters.Add(new SqlParameter("#TeamId", SqlDbType.Int));
// assign the search value to the parameter...
cmd.Parameters["#TeamId"].Value = teamId;
// create and add parameter to the collection...
cmd.Parameters.Add(new SqlParameter("#LoginId", SqlDbType.VarChar, 50));
// assign the search value to the parameter...
cmd.Parameters["#LoginId"].Value = loginId;
// set the command connection...
cmd.Connection = conn;
// open the connection...
conn.Open();
// perform deletion of user...
cmd.ExecuteNonQuery();
}
}
// bind control to refresh content...
ExtUsersGrid.DataBind();
}
Have I missed something obvious? I am happy to modify if there are better ways to do this.
Edit 1: Following on from the discussions below, I have modified the following:
Removed the Onclick event property of the ButtonItem;
Set the CommandName and CommandArgument as suggested below, and updated the DataKeyNames to use RowID which is a unique ID from the data;
Assigned a RowCommand event for the GridView;
Assigned the delete code to the RowCommand event.
Following these changes, it still fires the row event code on the second click.
Edit 2: FYI - I've now removed the SqlDataSource and the associated code/references, and created a procedure to fill the dataset, which is called on Page_Load (inside the !Page.IsPostBack brackets). I started making the changes below to use the RowCommand event, but they still caused the same issue (i.e. the button will only fire on the second click). As using RowCommand meant converting the BoundFields to ItemTemplates, I reverted back to the button click event as it seemed pointless making all those changes for no gain. If anyone else can help me understand why it only triggers on the second click, would appreciate your input.
OK, frustratingly this was due to some code that for reasons still unknown, works elsewhere.
In the DataBound event, there was two lines of code:
// get the associate name for the selected record...
var selectedId = DataBinder.Eval(e.Row.DataItem, "RowID").ToString();
// create unique row ID...
e.Row.ID = string.Format("ExtUserRow{0}", selectedId);
The process of applying an ID to the rows programatically seems to break the connection between the data and the events.
By removing these two lines of code, it works as expected.
Well instead you can do something like this.
Add a CommandName property to your GridView like this. Also note the changes in the CommandArgument property:
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" CssClass="additionsRow" ID="btnDeleteExtUser"
CausesValidation="False" Text="Delete"
CommandName="OnDelete"
CommandArgument='<%# Container.DisplayIndex %>" '
</ItemTemplate>
</asp:TemplateField>
Code behind will look something like this. Note that I am using the RowCommand event of Gridview.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "OnDelete")
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
// now you got the rowindex, write your deleting logic here.
int ID = Convert.ToInt32(myGrid.DataKeys[rowIndex]["ID"].Value);
// id will return you the id of the row you want to delete
TextBox tb = (TextBox)GridView1.Rows[rowIndex].FindControl("textboxid");
string text = tb.Text;
// This way you can find and fetch the properties of controls inside a `GridView`. Just that it should be within `ItemTemplate`.
}
}
Note: mention DataKeyNames="ID" inside your GridView. The "ID" is the primary key column of your table.
Are you binding the GridView on pageload? If so, then move it to !IsPostBack block as show below:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = yourDataSource;
GridView1.DataBind();
}
}

Categories