I have a gridview and within the item template i have a usercontrol which is losing it's value on postback. I want to be able to access the control from my RowDataBound method in the c# code behind and reassign the CandidateID value
GridView Control
<asp:TemplateField>
<ItemTemplate>
<Controls:LikeButton ID="CandidateLikeButton" runat="server" LikeType="Candidate"
LikedObjectID='<%# Bind("CandidateID") %>' />
</ItemTemplate>
</asp:TemplateField>
How can i do this?
You can access it with FindControl like any other Control and cast it back to it's type.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LikeButton LB = e.Row.FindControl("CandidateLikeButton") as LikeButton;
LB.CandidateID = 1234;
}
}
Related
I have GridView filled automatically as
<asp:GridView ID="gvValues" runat="server"
OnRowDataBound="gvValues_RowDataBound"
OnPageIndexChanging="gvValues_PageIndexChanging"
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# gvValues.PageSize*gvValues.PageIndex+ Container.DisplayIndex+1 %>
<asp:CheckBox ID="chkProduct" runat="server" CssClass="chkProduct"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="online" meta:resourcekey="Online">
<ItemTemplate >
<asp:CheckBox ID="chkProductonline" runat="server" OnCheckedChanged ="chkProductonline_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
What I need is when I click on the chkProductonline checkbox, to fire an event and get the chkProductonline and chkProducton values. I have tried this but it always gives me null.
protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
var chkProductonline = FindControl("chkProductonline") as CheckBox;
// bool ischeck = chkProductonline.Checked;
var chkProduct = gvValues.FindControl("chkProduct") as CheckBox;
}
I can't loop the GridView. I need to do this one-by-one. Is there another way to do that?
You can try this:
protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkProductonline = sender as CheckBox;
...
CheckBox chkProduct = chkProductionLine.NamingContainer.FindControl("chkProduct") as CheckBox;
...
}
You need to call FindControl on a specific row. You will not be able to call it on the entire GridView because there exists repeating content (i.e. multiple chkProductionlines and chkProducts). A row knows of its checkboxes, and not of the others.
So what you can do is first get the CheckBox that called the event (your sender parameter, chkProductionline) and use its NamingContainer. Since it is contained in a GridView row, cast the row as such as use it to find the other controls you may need.
protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkProductionline = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkProductionline.NamingContainer;
CheckBox chkProduct = (CheckBox)row.FindControl("chkProduct");
}
I have read many posts here looking for the answer. But no matter what I do I always get a null value returned for my Label.
I can populate it just fine and read it at that time, but when I try to read the label to populate DB it is always null.
aspx code:
<asp:TemplateField HeaderText="TOTAL YIELD" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="130">
<EditItemTemplate>
<asp:Label ID="lb_TotalYield" runat="server" ></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
in the code behind I am just trying to read it.
foreach (GridViewRow currentRow in gv_Fruit.Rows)
{
Label tempLabel = (Label)currentRow.FindControl("lb_TotalValue") as Label;
string theTotalValue = tempLabel.Text;
}
for complete information Here is how I set the label:
myGridView.SelectedRow.Cells[4].Text = myTotalYield;
I have tried doing a gv_RowDataBound, but it doesn't seem to ever be called.
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (myGridView.EditIndex == e.Row.RowIndex &&
e.Row.RowType == DataControlRowType.DataRow)
{
Label mylabel = (Label)e.Row.FindControl("lb_TotalYield");
mylabel.DataBind();
}
}
I am using a LinqDataSource and a FormView with paging enabled on an ASP.NET page. I am trying to access the FormView's DataItem property on PageLoad and I have no trouble on the first page load, but as soon as I use the Next/Prev page button (causing a postback) on the FormView the DataItem property is null, even if there a record showing in the FormView. Any ideas why it works fine on the first page load but not on a postback?
If you're curious what my PageLoad event looks like, here it is:
protected void Page_Load(object sender, EventArgs e)
{
Label lbl = (Label)fvData.FindControl("AREALabel");
if (fvData.DataItem != null && lbl != null)
{
INSTRUMENT_LOOP_DESCRIPTION record = (INSTRUMENT_LOOP_DESCRIPTION)fvData.DataItem;
var area = db.AREAs.SingleOrDefault(q => q.AREA1 == record.AREA);
if (area != null)
lbl.Text = area.AREA_NAME;
}
}
The object you bind to any data-bound control won't be persisted in the page's ViewState
Therefore, on subsequent posts the DataItem property will be null unless you re-bind the control
This property will contain a reference to the object when the control is bound.
Usually you would need to access this property if you want to do something when the objects is bound, so you need to react to the DataBound event
Example:
Output
Code behind
protected void ds_DataBound(object sender, EventArgs e)
{
var d = this.fv.DataItem as employee;
this.lbl.Text = d.lname;
}
ASPX
<asp:LinqDataSource ID="lds" runat="server"
ContextTypeName="DataClassesDataContext"
TableName="employees"
>
</asp:LinqDataSource>
<asp:FormView runat="server" ID="fv" DataSourceID="lds" AllowPaging="true"
OnDataBound="ds_DataBound">
<ItemTemplate>
<asp:TextBox Text='<%# Bind("fname") %>' runat="server" ID="txt" />
</ItemTemplate>
</asp:FormView>
<br />
<asp:Label ID="lbl" runat="server" />
Your data will not be preserved on PostBack. You'll need to rebind the FormView in the PageIndexChanging event using something like:
protected void FormView_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
FormView.PageIndex = e.NewPageIndex;
//rebind your data here
}
I want to make the gridview.columns[0] as hyperlink. I tried so many work around mentioned in different sites. I am binding a list<> to the grid. and I need to make the first column as hyperlink and upon clicking that link, it should be redirected to a page with the corresponding item.
Which event I need to use and how can I pass that value from the list?
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[0];
firstCell.Controls.Clear();
firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = firstCell.Text });
}
}
Be warned that if you bind data to grid only first time page loaded then your changes will disappear.
You have to make that column as Template Column
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text="test" NavigateUrl='<%# Eval("fieldName", "show.aspx?ID={0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I have this itemtemplate for a gridview column that is pulling data from a SQL database. My Question is how would I perform a check to see if my field ActivityFile has a value (which means a file is in the db) and then display the LinkButton at which point I generate code to download the file (already done and works).
<ItemTemplate>
<asp:LinkButton ID="DownloadFileBtn" runat="server" visible="false">Download File</asp:LinkButton>
<br />
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ActivityLink") %>'></asp:Label>
</ItemTemplate>
you have to use GridView RowDataBound Event for that
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
if (Convert.ToBoolean(dr["columnName"].ToString()))
{
LinkButton LinkButton = (LinkButton)e.Row.Findcontrol("LinkButton");
LinkButton.Visible = false;
}
}
}