Hidden template field value not avaialble on server Side - c#

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;
}

Related

how will i transfer a value from a gridview of a previous page to another web form?

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);
}
}

How to pass an Eval method through the attributes.add property?

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 + ");' %>");

How to get the modified textbox value in c#?

I used a textbox in gridview to bind a value from db during pageload..
The problem is when i change the textbox value i could not get the modified value in c#, instead it gives the original value which had been there before i modified..
I am using nested GridViews...
kindly help me..
<asp:TemplateField HeaderText="Singles" >
<ItemTemplate>
<asp:HiddenField ID="hidqua" runat="server" Value='<%#bind("QualityName") %>' />
<asp:HiddenField ID="hidfau" runat="server" Value='<%#bind("FaultName") %>' />
<asp:TextBox ID="asptxtsingleg" runat="server" Text='<%# bind("singles") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
and below is my c# Coding
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "update")
{
foreach (GridViewRow row in grdInspection.Rows)
{
HiddenField hv = (HiddenField)row.FindControl("hidval");
GridView txtsi = (GridView)row.FindControl("grdInsiewChid");
foreach (GridViewRow row1 in txtsi.Rows)
{
HiddenField htn = (HiddenField)row1.FindControl("hdnPLength");
GridView nesgrid = (GridView)row1.FindControl("GridView1");
foreach (GridViewRow row2 in nesgrid.Rows)
{
HiddenField qn = (HiddenField)row2.FindControl("hidqua");
TextBox t = (TextBox)row2.FindControl("asptxtsingleg");
}
}
}
}
}
Assuming you are using button to fire the RowCommand event.
<asp:TemplateField HeaderText="Singles">
<ItemTemplate>
<asp:TextBox ID="asptxtsingleg" runat="server" Text='<%# Eval("singles") %>' Width="120px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="BtnEdit" runat="server" Text="Update" CommandName="updateData" CommandArgument='<%# Eval("Your_ID") %>' />
</ItemTemplate>
</asp:TemplateField>
Code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "updateData")
{
//int i = Convert.ToInt32(e.CommandArgument);
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
TextBox tb = (TextBox)row.FindControl("asptxtsingleg");
}
}
You can use below code to get the text box value in RowUpdating event
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox t= (TextBox)row.FindControl("asptxtsingleg");

C# getting selected value from dropdownlist in gridview asp net

How can I change the value of a textbox whenever a dropdownlist within a gridview has its value changed?
On page load, the textbox shows the selected value, but when I change the selection of the dropdownlist, the textbox value doesn't change.
The code is below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:TemplateField HeaderText="Entry">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duty">
<ItemTemplate>
<asp:DropDownList ID="duty" runat="server" OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" autopostback="true" EnableViewState="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The code behind is below.
protected void ddl1_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
Duty dy = new Duty();
dt = dy.getdutyid(Convert.ToInt32(dropcontractid.SelectedValue));
DropDownList ddl = (DropDownList)sender;
ddl.DataSource = dt;
ddl.DataTextField = "dutyid";
ddl.DataValueField = "dutyid";
ddl.DataBind();
TextBox1.Text = ddl.SelectedValue;
}
}
You need to use SelectedIndexChanged handler to show selected value:
Markup:
<asp:DropDownList ID="duty" runat="server" OnLoad="ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged"></asp:DropDownList>
Code-behind:
protected void duty_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
DropDownList duty= (DropDownList) gvr.FindControl("duty");
TextBox1.Text = duty.SelectedItem.Value;
}
I had a similar problem using the DropDownLists in GridView. My solution was to adjust the onLoad for the dropdown so that it wouldn't re-write the DropDownList on every post back. This way if there's something there then it won't re-populate it.
protected void dropDownLoad(object sender, EventArgs e)
{
DropDownList dropDown = sender as DropDownList;
if (dropDown.SelectedValue == null || dropDown.SelectedValue == "")
{
// Your Code to populate table
}
}
You should look into using data binding instead. You can bind the textbox.Text to the selecteditem.value, this will ensure that proper updating takes place
this happens to me once then i code like this... but i didnt use the onLoad attribute, tell me if this works,
<asp:TemplateField HeaderText="duty" SortExpression="duty">
<EditItemTemplate>
<asp:TextBox ID="duty" runat="server" Text='<%# Bind("duty_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblduty" runat="server" Text='<%# Eval("duty_Name") %>' />
<asp:DropDownList ID="ddlduty" runat="server" CssClass="dropdownlist"
OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" Visible = "false"
>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="5%" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

How can I select all data from GridView's current row

How can I select all data from GridViews current row..
I have a column for edit link in GridView. When "Edit" link button is clicked, I want to use that selected row's data. I am trying the following code, but it's returning me with an empty value
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = -1;
GridViewRow gvRow = gv.Rows[ e.NewEditIndex];
string selectedID = gvRow.Cells[3].Text;
}
<asp:GridView runat = "server" ID="gvRange0" SkinID="gridView" AutoGenerateColumns="False"
AllowSorting="True" OnRowCancelingEdit="gvRange_RowCancelingEdit" OnRowDeleting="gvRange_RowDeleting"
OnRowEditing="gvRange_RowEditing" OnRowUpdating="gvRange_RowUpdating"
Width="684px" OnRowDataBound="gvRange_RowDataBound"
DataMember="DefaultView" OnPageIndexChanged="gvRange_PageIndexChanged"
OnPageIndexChanging="gvRange_PageIndexChanging" OnSorting="gvRange_Sorting" DataKeyNames = "RANGE_ID"
OnSelectedIndexChanged="gvRange_SelectedIndexChanged" Height="65px" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<ControlStyle Width="2px" />
<asp:LinkButton ID="lnkDelete0" runat="server" CssClass="lnk"
CausesValidation="False" CommandName="Delete"
Text="Delete" Visible="false"></asp:LinkButton>
<asp:CheckBox runat="server" ID="chkSelect" CssClass="lbl" Text="" AutoPostBack="False" OnCheckedChanged="chkSelect_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<controlStyle width="2px" />
<asp:LinkButton ID="lnkEdit" runat="server" CssClass="lnk" CausesValidation="False" CommandName="Edit"
Text="Edit" ></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="5px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Ranges" SortExpression="Sort_Ranges">
<ControlStyle Width="5px" />
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"Min_Age") %>
<%# CheckNull(DataBinder.Eval(Container.DataItem,"Max_Age")) %>
</ItemTemplate>
<%-- <ItemTemplate>--%>
<%--<asp:Label ID="lblStageName" CssClass="lbl" runat="server" Text='<%# Bind("Age_Range") %>' Width="1px"></asp:Label>--%>
<%-- </ItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Range ID">
<ItemTemplate><%#DataBinder.Eval(Container.DataItem,"RANGE_ID") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
There are 4 columns in the GridView. One contains check box, second is link button for edit, third databound with some value, fourth is the column which I want to use to get some values from database(that is primary key there) and this column is hidden.
sometime in gridview cell create child controls.
you can try this code. may be solve it.
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = -1;
GridViewRow gvRow= (GridViewRow)(((Button)e.CommandSource).NamingContainer);
foreach (TableCell Tc in gvRow.Cells)
{
//if you are not getting value than find childcontrol of TabelCell.
string sss = c.Text;
foreach (Control ctl in Tc.Controls)
{
//Child controls
Label lb = ctl as Label;
string s = lb.Text;
sb.Append(s + ',');
}
}
}
I've noticed that you say you need to access the 4th column but you're using gvRow.Cells[3].Text;
The indexing in the Cell object is done from 1, so if you need to access the 4th row in the grid view try this:
string selectedID = gvRow.Cells[4].Text;
EDIT:
Could you please confirm two things for me
1)When you click on lnkEdit is the GridView1_RowEditing event being raised?
2)If yes, is the e.NewEditIndex value always showing up as '0'?
Attempt clicking the edit link on different rows, is the result always '0'?

Categories

Resources