How can I get the GridView object in the RowDataBound event?

M

mark4asp

I have two grid views on the page and I want them to share the same
RowDataBound event.

In debug mode in notice that sender and gvMandates (below) are both
termed {System.Web.UI.WebControls.GridView} when I add a watch and
that they show more or less the same properties and methods (depending
upon whether they are the same gridview). However when coding using
intellisense gvMandates shows all the properties and methods available
to it while sender has only 4 methods, equals(), GetHashCode(),
GetType(), ToString(). Further, if I substitute sender for gvMandates
in the code, compilation fails.

protected void GridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = "<a href='javascript:InflowDetails("
+ gvMandates.DataKeys[e.Row.DataItemIndex].Value + ");'>"
+ e.Row.Cells[0].Text + "</a>";
sender.
}
}

This is not just a moan for consistency but also a plea. How can find
out what the actual GridView object is? Do I really need to make a
GridView object up (see below) or is there a more elegant way I could
do this?

protected void GridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (sender.Equals(gvMandates)) ? gvMandates :
gvMandateTotals;
e.Row.Cells[0].Text = "<a href='javascript:InflowDetails(" +
gv.DataKeys[e.Row.DataItemIndex].Value + ");'>" + e.Row.Cells[0].Text
+ "</a>";
}
}
 
Joined
Oct 28, 2010
Messages
1
Reaction score
0
Find and enable or disable control on rowbound in gridview

<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="img_Update" runat="server" CausesValidation="True" CommandName="Update"
/>
<asp:ImageButton ID="img_Cancel" runat="server" CausesValidation="False" CommandName="Cancel"
/>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="5%" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:ImageButton ID="img_Edit" runat="server" CausesValidation="False" CommandName="Edit"
ImageUrl="~/Images/edit.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="5%" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate >
<asp:ImageButton ID="img_Delete" runat="server" CausesValidation="False" CommandName="Delete"
ImageUrl="~/Images/delete.png" />
</ItemTemplate>
</asp:TemplateField>



string maxid = "500";//or u may asign by viewstate ;
ImageButton lbEdit;
lbEdit = (ImageButton)e.Row.Cells[0].FindControl("img_Edit");
if (lbEdit != null)
lbEdit.Attributes.Add("OnClick", "return confirm('Are you sure to Edit this Record?');");


ImageButton lb;
lb = (ImageButton)e.Row.Cells[0].FindControl("img_Delete");
if (lb != null)
lb.Attributes.Add("OnClick", "return confirm('Are you sure to delete this Record?');");
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[5].Text == "Y")
e.Row.Cells[5].Text = "Yes";
else
e.Row.Cells[5].Text = "No";

if (e.Row.Cells[8].Text == maxid.Trim())
lbEdit.Enabled = true;
else
lbEdit.Enabled = false;

e.Row.Cells[8].Visible = false;


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top