I have a Gridview with all the rows as textboxes and one edit button in the last column. All the textboxes have an onclick property which calls a javascript method. Here is the HTML code of one of the textbox:
<asp:TextBox ID="txtLoginName" Onclick='<%# "pass(" + Eval("userid") + ");" %>'
Text='<%# Eval("LoginName")%>' runat="server"></asp:TextBox>
When a user clicks on the Edit button, the onclick property is removed and the button changes to Save. After making the changes, when a user clicks Save, I want to re-add the onclick property again. This is the code I have tried:
protected void btnEdit_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
TextBox lstTxt = (TextBox)row.FindControl("txtLoginName");
if (btn.Text == "Edit")
{
lstTxt.Attributes.Remove("onclick");
btn.Text = "Save";
}
else
{
lstTxt.Attributes.Add("onclick", "<%# 'pass(" + Eval("userid") + ");' %>");
btn.Text = "Edit";
}
But its showing error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control
How to pass that correctly?
You have to get the userid from sonmewhere else. You could store it for example in a HiddenField or invisible label and use e.Row.FindControl to get this control. Another approach is using the button's CommandArgument.
aspx:
<asp:Button ID="btnEdit" runat="server" CommandName="Edit" CommandArgument='<%# Eval("userid") %>'>
codebehind:
string userID = btn.CommandArgument.ToString();
lstTxt.Attributes.Add("onclick", "pass(" + userID + ");");
Here is the approach with a control like HiddenField:
<asp:TemplateField HeaderText="UserID" Visible="false">
<ItemTemplate>
<asp:HiddenField ID="HidUserID" Value='<%#Eval("userid") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
Now you can get it from codebehind via FindControl:
HiddenField hidUserID = (HiddenField)e.Row.FindControl("HidUserID");
string userID = hidUserID.Value;
You can use like this:
use it in grid view template field
<asp:TemplateField HeaderText="userid" Visible="false">
<ItemTemplate>
<asp:Label runat="server" id="lblUserid" style="display:none" Text='<%# Eval("UserID") %>' />
</ItemTemplate>
</asp:TemplateField>
find in code behind
GridViewRow row = (GridViewRow)btn.NamingContainer;
Label lbl = (TextBox)row.FindControl("lblUserid");
string userid=lblUserid.Text;
lstTxt.Attributes.Add("onclick", "<%# 'pass(" + userid + ");' %>");
Related
I have created a gridview using TemplateField. And Added Add Button And this redirects to other page and i want to get the one column value of row.
Here is the code -
<Columns>
<asp:TemplateField HeaderText="Accession ID">
<ItemTemplate>
<asp:Label Text='<%# Eval("AccessionID") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" Text="Hello" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstNameFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
......
<asp:TemplateField HeaderText="Add Container">
<ItemTemplate>
<asp:Button ID="btnAddContainer" runat="server" CommandName="" Text="Add Container" CommandArgument='<%# Eval("AccessionID") %>' OnClick="ContainerforAccession" class="btn btn-primary btn-md" />
</ItemTemplate>
</asp:TemplateField>
CS Code -
protected void ContainerforAccession(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
}
I have this code with me. I want to get first column value (AccessionID) in my case in every button click of the Grid.
I have tried across multiple solutions. But unfortunately, we have the answers for 'asp:BoundField' Implementation. I am looking for 'asp:TemplateField'
Please let me know if i need to convert to asp:BoundField. If so, How can the above design can be changed - Binding Text='<%# Eval("AccessionID") %> to Textbox.
Thanks in advance. Looking for a solution.
Change the OnClick of the button to OnCommand. Then you have access to the CommandEventArgs.
<asp:Button ID="btnAddContainer" OnCommand="ContainerforAccession"
protected void ContainerforAccession(object sender, CommandEventArgs e)
{
string AccessionID = e.CommandArgument.ToString();
}
Changed All the Template fields to Bound-Fields and in cs code -
int AccessionID = Int32.Parse((sender as Button).CommandArgument);
Solved my issue !
how can i pass the value from the gridview to the next webform? here is my gridview code below:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID ="lnkEdit" runat="server" Text="View" PostBackUrl='<%# "Details.aspx?RowIndex=" + Container.DataItemIndex %>'></asp:LinkButton>
</ItemTemplate
</asp:TemplateField>
but i can't seem to make it work. it goes to the next form but does not get the value. my id has a format "1000001" and it does read it.
here is my code for the next page. i created a new gridview just to store the value from.
int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
GridView gv1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
GridViewRow row = GridView1.Rows[rowIndex];
lblID.Text = row.Cells[0].Text;
Add OnRowCommand="GridView_RowCommand" like
<asp:GridView ID="GridView" OnRowCommand="GridView_RowCommand"
Update your Columns as
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID ="lnkEdit" runat="server" Text="View" CommandName= "Redirect" CommandArgument='<%#Eval("ID")%>' ></asp:LinkButton>
</ItemTemplate
In Code-Behind file
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Redirect") {
//Get Command Argument
Response.Redirect("Details.aspx?RowIndex="+e.CommandArgument.ToString(),true);
}
}
I am working on Asp.net and stuck in middle.
I have a gridview with template field.Gridview have 3 columns and I want to pass value of two columns to server Side.
Scenario-On Clicking lnkRemove(LinkButton),the GUID should be passed(which is getting passed in Command arguement) and also the value of SEQ_NBR column(passing this value is problem for me)
I am trying with Hidden field but how to get hiiden field value in Server Side code(i.e Deletedata).
Code
<asp:GridView>
<Columns>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "SEQ_NBR">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("SEQ_NBR") %>' />
<asp:Label ID="SEQ_NBR" runat="server"
Text='<%# Eval("SEQ_NBR")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="SEQ_NBR" runat="server" ReadOnly="true" Text="Auto generated"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server"
CommandArgument = '<%# Eval("GUID")%>'
OnClientClick = "return confirm('Do you want to delete?')"
Text = "Delete" OnClick = "Deletedata"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And my server side code is:
protected void Deletedata(object sender, EventArgs e)
{
//I want Hiddden field value here;I tried below code but not working.Any suggestion .
chkSelect = GridView.Controls[0].Controls[0].FindControl("HiddenField1");
}
Don't use
GridView.Controls[0].Controls[0].FindControl("HiddenField1");
but on the GridViewRow since that is the NamingContainer:
GridView.Rows[0].FindControl("HiddenField1");
But in this case you want to find the hiddenfield from the LinkButton's click-event. Therefore use following approach. The LinkButton's NamingContainer is the GridViewRow:
protected void Deletedata(object sender, EventArgs e)
{
LinkButton lnkRemove = (LinkButton) sender;
GridViewRow row = (GridViewRow) lnkRemove.NamingContainer;
HiddenField hf = (HiddenField) row.FindControl("HiddenField1");
string seqNbr = hf.Value; // voilĂ
}
Try this one
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
GridView.SelectedIndex = row.RowIndex;
var HiddenField= GridView.Rows[gridMain.SelectedIndex].FindControl("HiddenField1") as HtmlInputHidden;
if (HiddenField!= null)
{
sting strValue = HiddenField.Value;
}
What should I do to fix this? html inside a asp:button need a fix to be displayed, I use this method (it works unless I do click on the header of any column to sort the gridview):
private void FixGlyph(PlaceHolder ph, Button btn, string iconClass, string customLabelStye = "")
{
if (btn.Visible)
{
var g = new HtmlGenericControl();
g.ID = "labelFor_" + btn.ID;
g.TagName = "label";
g.Attributes.Add("for", btn.ClientID);
g.Attributes.Add("class", "" + btn.CssClass + "");
if (customLabelStye != "")
{
g.Attributes.Add("style", customLabelStye);
}
g.InnerHtml = "<i class='" + iconClass + "'></i> " + btn.Text;
ph.Controls.Add(g);
btn.Attributes.Add("style", "display:none;");
}
}
my gridview is this:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" AllowPaging="True" AutoGenerateColumns="false" DataSourceID="AccessDataSource1" DataKeyNames="FICHA" runat="server" GridLines="None" OnSorted="GridViewFix" AllowSorting="true" CssClass="table table-hover table-striped" ShowFooter="true" PagerStyle-CssClass="bs-pagination text-center">
<Columns>
<%-- Ficha del empleado --%>
<asp:TemplateField HeaderText="Ficha" SortExpression="FICHA">
<ItemTemplate>
<asp:Label ID="lblFicha" Text='<%# Eval("FICHA") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%-- columna de eliminar --%>
<asp:TemplateField HeaderText="<span class='glyphicon glyphicon-remove'></span>">
<ItemStyle Width="20" />
<ItemTemplate>
<asp:PlaceHolder ID="delPh" runat="server"></asp:PlaceHolder>
<asp:Button ID="delBtn" CssClass="btn btn-link " data-toggle="modal" ToolTip="Modify" data-target="#deleteModal" runat="server" autopostback="false" OnClick="delBtn_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
my Page_Load is like this
protected void Page_Load(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Button dbtn = row.FindControl("delBtn") as Button;
PlaceHolder dPH = row.FindControl("delPh") as PlaceHolder;
FixGlyph(dPH, dbtn, "glyphicon glyphicon-remove", "padding:0 !important;");
}
}
and I even tried to use onSorted event for the gridview:
protected void GridViewFix(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Button dbtn = row.FindControl("delBtn") as Button;
PlaceHolder dPH = row.FindControl("delPh") as PlaceHolder;
FixGlyph(dPH, dbtn, "glyphicon glyphicon-remove", "padding:0 !important;");
}
}
which gets trigged, aparently finds the controls and fix the labels but... then something happend after GridViewFix and reloads the components, again without the glyphicons
I would put your code to add the glyphs in the Page_PreRender method. It's possible that after page_load the Gridview has an event that is firing and recreating the button controls.
well, the question wasn't completly clear.. buttons have data-toggle and data-target atributes which LinkButtons don't and that's why wanted them so bad. I Needed to open a modal at the same time I triggered an event on code behind. so this fix the issue: Just trigger the modal yourself with javascript.
<asp:LinkButton ID="AddAreaBtn" runat="server" autopostback="false" OnClientClick="javascript:$('#BorrarAreaModal').modal('show');" OnClick="BorraArea_Click">
<i aria-hidden="true" class="glyphicon glyphicon-remove"></i>
</asp:LinkButton>
<ItemTemplate>
<asp:TextBox ID="Q1" runat="server" Text='<%# Bind("Q1") %>'></asp:TextBox>
</ItemTemplate>
.
.
.
<ItemTemplate>
<asp:TextBox ID="Q2" runat="server" Text='<%# Bind("Q2") %>'></asp:TextBox>
</ItemTemplate>
I currently have a page with fields as textbox and I would like to change some of them label based on condition in code behind.
For example if Window_name='Q2' --> make Q2 Q3 Q4 textbox and Q1 label
if it is Window_name='Q3' make Q3 and Q4 textbox but Q1 and Q2 label
Btw, I'm not using edit/select gridview modes as I made it bulk update gridview (one button to update all rows)
I'm trying to help you with a sample of two controls and sample grid view ID 'GridView1' here, alter it according to your code:
You could either have the labels created instead of showing the text box in the CODE BEHIND or create both textboxes and labels initially and show them when needed.
Also instead of doing it in the Page_Load function you could do it in the 'RowDataBound' event of GridView and bind the GridView each every time a post back is done.
ASPX Code:
<ItemTemplate>
<asp:TextBox ID="Q1" runat="server" Text='<%# Bind("Q1") %>'></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Q1") %>' Visible="false">
</asp:Label>
</ItemTemplate>
.....
<ItemTemplate>
<asp:TextBox ID="Q2" runat="server" Text='<%# Bind("Q2") %>'></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Q2") %>' Visible="false">
</asp:Label>
</ItemTemplate>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
//Bind your grid view
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int rowIndex = e.Row.RowIndex;
//First fetch your textboxes and labeles
TextBox textBoxQ1 = GridView1.Rows[rowIndex].FindControl("Q1") as TextBox;
TextBox textBoxQ2 = GridView1.Rows[rowIndex].FindControl("Q2") as TextBox;
Label Label1 = GridView1.Rows[rowIndex].FindControl("Label1") as Label;
Label Label2 = GridView1.Rows[rowIndex].FindControl("Label2") as Label;
if (Window_name.Equals("Q2"))
{
//Set 'visiblity' to 'true' for those LABEL you want to show. Sample one below
Label2.Visible = false;
//Set 'visibilty' to 'false' for those TEXT BOXES you want to hide. Sample one below
textBoxQ2.Visible = false;
}
}
Let me know in case of any queries.