I have GridView with Template Column.Inside the template column i have asp:hiddenfield. I am binding the value using Eval() method.When i am trying to access the value of hiddenfi not accesible while visibility false
ASPX
<asp:TemplateField HeaderText="Select" Visible="false">
<ItemTemplate>
<asp:HiddenField ID="hdnMasterId" runat="server"
Value='<%# DataBinder.Eval(Container.DataItem, "Master_Id") %>' />
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
<ItemStyle Width="4%" HorizontalAlign="Center"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
CODE BEHIND
protected void gdvList_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gdvList.Rows[intIndex];
HiddenField hdn = (HiddenField)row.FindControl("hdnMasterId");
}
If you set visibility="false" on a column it won't generate any html, thus wont have the hidden control. You need to put the hiddenfield elsewhere or hide the column with css/style instead.
You could try as:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string strValue = ((HiddenField)GridView1.SelectedRow.Cells[cellindex].FindControl("HiddenFieldID")).Value;
}
Related
I have grid view.there are two BoundField.here i want to set tooltip on BoundField DataField HeaderText Topic.
code.
<asp:GridView ID="Dgvlist" runat="server" >
<Columns>
<asp:BoundField DataField="topic" HeaderText="Topic" />
<asp:BoundField DataField="question" HeaderText="Question" />
</Columns>
</asp:GridView>
there are any solution?
There are 3 usual ways to set tooltip on BoundField column:
1) Using code-behind RowDataBound event
protected void Dgvlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Cells[0].ToolTip = DataBinder.Eval(e.Row.DataItem, "Topic", string.Empty);
}
}
2) Using code-behind RowCreated event
protected void Dgvlist_RowCreated(object sender, GridViewRowEventArgs e)
{
foreach (TableRow row in Dgvlist.Controls[0].Controls)
{
row.Cells[0].ToolTip = DataBinder.Eval(e.Row.DataItem, "Topic", string.Empty);
}
}
3) Convert to TemplateField and use Label control
<asp:GridView ID="Dgvlist" runat="server" ...>
<Columns>
<asp:TemplateField HeaderText="Topic">
<asp:Label ID="TopicID" runat="server" Text='<%# Eval("topic") %>' ToolTip='<%# Eval("topic") %>'>
</asp:Label>
</asp:TemplateField>
<asp:BoundField DataField="question" HeaderText="Question" />
</Columns>
</asp:GridView>
The actual implementation depends on what method you're using.
Related issue:
How to add tooltip to BoundField
One hacky way to achieve this is to convert your BoundField to TemplateField option.
Convert this:
<asp:BoundField DataField="topic" HeaderText="Topic" />
To this:
<asp:TemplateField HeaderText="Topic">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Topic") %>' ToolTip ='<%# Bind("Topic") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Or From code behind you can do it in RowDataBound event like this
protected void Dgvlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;
}
}
i have a gridview ,whenever user will click on row of gridview view ,
record will be populated in the textbox ,but i am getting error as
index was out of range. must be non-negative and less than the size of
the collection
also i check gridview.row.count is showing zero.
please help below is my code
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" ShowHeader="false" AllowPaging="false" AllowSorting="false" ToolTip="Click Here To Edit"
Style="table-layout: fixed;" OnRowDataBound="GridView1_RowDataBound"
CssClass="mGrid" PagerStyle-CssClass="pgr" DataKeyNames="AcheadID"
AlternatingRowStyle-CssClass="alt" Width="100%" Height="100%" >
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField ItemStyle-Width="40px">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Account Head ID" DataField="AcheadID" HeaderStyle-HorizontalAlign="Left" Visible="false" />
<asp:TemplateField HeaderText="Account Head" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="400px">
<ItemTemplate>
<asp:Label ID="lblac_head" runat="server" Text='<%# Bind("ac_head") %>'></asp:Label>
<asp:HiddenField ID="hndacheadID" runat="server" Value='<%# Bind("AcheadID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnTest" runat="server" OnClick="GridView1_OnClick" style="display:none;" />
my codebehind as
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetAccountHead();
}
}
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hndac_headid = (HiddenField)e.Row.FindControl("hndac_headid");
if (hndac_headid.Value != "0")
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d3d3d3'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
//e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnTest, e.Row.RowIndex.ToString());
}
}
}
protected void GridView1_OnClick(object sender, EventArgs e)
{
Button buttonSender = sender as Button;
int index;
GridViewRow row = null;
if (int.TryParse(Request.Params.Get("__EVENTARGUMENT"), out index))
{
row = GridView1.Rows[index];
Label ac_head = (Label)GridView1.Rows[index].FindControl("lblac_head");
}
Index out of range issue happens because Request.Params.Get("__EVENTARGUMENT") doesn't works for Button ( and addiionally ImageButton ) controls.
The Button controls and ImageButton controls does not call the __doPostBack function. Because of this, the _EVENTARGUMENT ( and _EVENTTARGET as well ) will always be empty.
However, other controls such as CheckBoxes, DropDownLists, LinkButtons uses javascript function __doPostBack to trigger a postback.
Try using a LinkButton
As a second check( can be ignored ), just make sure Data is binded to GridView properly like check the IsPostback conditions etc...
Check your GridView, it has an Id GridView2 but you always referencer GridView1.
I am having a grid view created by using template fields. I inserted a link button using template fields in the grid view.
There are 4 textboxes outside the GridView.. i want to select the row on link button's click and put the selected row's data in text boxes. I am using a row command even for this but its not working ... the syntax i am using is .:
<asp:GridView ID="gview" AutoGenerateColumns="False" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" onrowcommand="gview_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Book Name">
<ItemTemplate>
<%#Eval("book_name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Author">
<ItemTemplate>
<%#Eval("book_author") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Publisher">
<ItemTemplate>
<%#Eval("book_Publisher") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Price">
<ItemTemplate>
<%#Eval("book_Price") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Update">
<ItemTemplate>
<asp:LinkButton ID="lnkDet" CommandName="cmdBind" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" runat="server" CausesValidation="false">View Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
and Code behind file :
protected void gview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdBind")
{
LinkButton lb = (LinkButton)e.CommandSource;
int index = Convert.ToInt32(lb.CommandArgument);
//Bind values in the text box of the pop up control
txt_name.Text = gview.Rows[index].Cells[0].Text;
txt_author.Text = gview.Rows[index].Cells[1].Text;
txt_price.Text = gview.Rows[index].Cells[2].Text;
}
}
Can anyone tell me how to do this.
I think this works:
protected void gview_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName == "cmdBind")
{
int index = Convert.ToInt32(e.CommandArgument);
//Bind values in the text box of the pop up control
txt_name.Text = gview.Rows[index].Cells[0].Text;
txt_author.Text = gview.Rows[index].Cells[1].Text;
txt_price.Text = gview.Rows[index].Cells[2].Text;
}}
Attempting it the way you are is not impossible, but it may be easier to just use the in built select buttons for each row. That way you can just use the SelectedIndexChanged event of the gridview:
protected void gview_SelectedIndexChanged(object sender, EventArgs e)
{
txt_name.Text = gview.SelectedRow.Cells[0].Text;
txt_author.Text = gview.SelectedRow.Cells[1].Text;
txt_price.Text = gview.SelectedRow.Cells[2].Text;
}
I have this scenario.
An ascx contains this GridView:
<asp:GridView ID="dataTable" runat="server" >
<Columns>
<asp:TemplateField HeaderText="Key">
<ItemTemplate>
<%# ((KeyValuePair<string, string>)(Container.DataItem)).Key%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Value">
<ItemTemplate>
<%# ((KeyValuePair<string, string>)(Container.DataItem)).Value%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I'm loading a fake Array into the GridView during the Page_Load
var values = new Dictionary<string, string>();
values.Add("test1", "2");
values.Add("test2", "2");
dataTable.DataSource = values;
dataTable.DataBind();
If during the OnPreRender I try to check the value of
dataTable.Rows[0].Cells[0].Text
It has no value. Then the grid is rendered perfectly and every value is in place.
Is there any way to fix this?
You can put a label control like this:
<asp:TemplateField HeaderText="Key">
<ItemTemplate>
<asp:Label runat="server" Text="<%# ((KeyValuePair<string, string>)(Container.DataItem)).Key%>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And then access the value like this on the user control´s PreRender:
protected void Page_PreRender(object sender, EventArgs e)
{
Label lbl = dataTable.Rows[0].Cells[0].Controls[1] as Label;
string t = lbl.Text;
}
Or like this on the consumer page´s PreRender:
protected void Page_PreRender(object sender, EventArgs e)
{
GridView dataTable = dataTableWebUserControl1.FindControl("dataTable") as GridView;
Label lbl = dataTable.Rows[0].Cells[0].Controls[1] as Label;
string t = lbl.Text;
}
I have a Gridview
<asp:GridView ID="GridView1" runat="server" Width="400px" AutoGenerateColumns="false"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="lblStudentName" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblResidentialAddress" runat="server" Text='<%# Eval("ResidentialAddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and i get the value binded to the gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt = objdb.GetData("Getsamples", new object[] { });
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
Response.Write(GridView1.SelectedRow.Cells[0].Text);
// string selectedText = ((Label)GridView1.SelectedRow.FindControl("lblStudentName")).Text;
// Response.Write(selectedText);
}
i cannot able to retrive the row where the checkbox is checked...
How to select particular row in a gridview, and based upon the selection i need to take out the 'Name' and pass this as a parameter to get ,another gridview related to the row which i selected.???
any help...
Try using another event - OnSelectedIndexChanging (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanging.aspx)
It has GridViewSelectEventArgs (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewselecteventargs_members.aspx) passed to the event handler which has NewSelectedIndex property.
Your event handler will look like:
void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
}