I am using the below code to handle the gridview dropdownlist on change event in clientside. In the mean time i want to pass the row id for my further process. But it's not working. But it's working while i call javascript without id.How can i pass the id to javascript.
<asp:TemplateField ItemStyle-Width="10px" HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="ddl_status" runat="server" onchange='chng_status("<%#Bind("FLD_ID") %>")' >
<asp:ListItem></asp:ListItem>
<asp:ListItem>Realised</asp:ListItem>
<asp:ListItem>Non-Realised</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
function chng_status(value) {
alert('enter');
alert(value);
}
You can do that like this:
<asp:DropDownList ID="ddl_status" runat="server"
onchange='<%# Eval("FLD_ID","chng_status(\"{0}\");") %>' >
I am using string formatting to pass the value to a javascript function.
Note that i don't fully understand what you are trying to achieve
Related
I am using asp.net web forms and have following concern.
I have one aspx page having asp:DropDownList and Gridview with asp:HyperLinkField in each column.
So I want to set the value of selected DropDownList to hyperLinkField tag dynamically either code behind or inside the following code.
<asp:HyperLinkField Text="Merge" DataNavigateUrlFields="MemberId" Target="_blank" DataNavigateUrlFormatString='/MergeMember.aspx?memberid={0}&clubid={1}" />
As you can see I have bind the field called MemberId in {0} and now I want to set the selected value of dropdownlist in {1}. Setting {1} is just a demo to show where I really want to set the value of selected dropdownlist I have't implemented that yet.
If it is possible please guide how?
If it is not possible please guide me how I can achieve this using different approach?
Thanks in advance.
.aspx Page
<asp:DropDownList ID="ddlTest" runat="server">
</asp:DropDownList>
<asp:GridView ID="grdTesting" runat="server" AutoGenerateColumns="false" Width="98%">
<Columns>
<asp:TemplateField HeaderText="Job Number">
<ItemTemplate>
<asp:LinkButton ID="lnkTest" runat="server" Text='<% # Bind("Test") %>' ToolTip='<%# Bind("Test") %>'
OnClick="lnkTest_Click"></asp:LinkButton>
<asp:HiddenField ID="hdnId" runat="server" Value='<% # Bind("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs
protected void lnkTest_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((Control)sender).Parent.Parent;
HiddenField hdnId = (HiddenField)gvr.FindControl("hdnId");
Response.Redirect("frmTest.aspx?Id=" + Convert.ToString(hdnId.Value) + "&DropDownId=" + ddlTest.SelectedValue, false);
}
Note:-
I think you have to change your Logic.....
In above Example...I'm binding a Gridview with Linkbutton and Id
On click of link button you can redirect to other page(as per your Logic..)
with sending Query string value(Id and Dropdownlist value)
I'm using asp.net with c# 4.5
Get external dropdownlist value here
On client side button click event, I want to get control id that are place in Item template of Grid View. I tried this code but it doesn't work. Thanks
function buttonClicked(sender, args) {
var gv = $find('<%= GridView1.ClientID %>');
var textbox = $GridView1.findControl(gv.get_element().parentNode, "Textbox");
}
Here is the Gridview
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1"runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="upTest" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="KurzDS" DataKeyNames="Id" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="Textbox" runat="server" Text="Textbox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="Textbox1" runat="server" Text="Textbox1"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" Text='btn' CommandArgument='<%# Eval("Id")%>' CommandName="btn" runat="server" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Thanks for including the GridView example. Now that I can see what you are attempting, I have a much better answer for you.
First, make a slight change to the button template, change out CommandArgument for OnClientClickand since you are using this button client side instead of posting back to the server, you can simplify it like this:
<asp:Button ID="btn" Text='btn' OnClientClick='<%# Eval("ID", "YourJavascriptFunction({0} - 1); return false;") %>' runat="server" CausesValidation="false" />
I have the click event call your JavaScript function and it sends in a parameter of the server side resolved id. Notice I subtract 1 first though. This is because the server side ASP.Net Eval function give the ID starting at 1. But, each of the ids that get generated for your text input elements start with a zero base.
Now look at the JavaScript function below.
// Clicking the first button sends in a 0, second sends in a 1, etc.
function YourJavascriptFunction(id) {
// each of the TextBox1 elements has an ASP.Net server side
// generated id that ends with GridView2_Textbox1_0,
// GridView2_Textbox1_1, etc.
let selectId = "input[id$='GridView2_Textbox1_" + id + "']";
// The text we use in the querySelector function states
// find the DOM element with a tag of "input" where the id
// ends with . . . The $ in id$= is the part that says the
// value must "end with"
let textBox1 = document.querySelector(selectId);
// Now that we have TextBox1 from the same row as the button,
// getting the value is easy.
alert(textBox1.value);
}
I left off a jQuery example as this querySelector command works in almost every browser including IE8 and above, so you shouldn't need jQuery for something this simple.
Let me know if I can help further.
Here is a code of template field in gridview . i want to get value of this field in string using c# code.
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="P"></asp:ListItem>
<asp:ListItem Value="L"></asp:ListItem>
<asp:ListItem Value="A"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
i tried the following code but its not working .
here is my code for reading value.
string str = gridview1.Rows[0].Cells[0].Text.ToString();
but its returning empty string. how can I get selected value.
You can't find the dropdown selected value like this. You need to find the control first using FindControl method:-
DropDownList DropDownList2 = (DropDownList)gridview1.Rows[0].Cells[0]
.FindControl("DropDownList2");
string str = DropDownList2.SelectedValue;
I am having trouble retrieving the new value that is entered in a textbox template field in my GridView.
Here is my markup:
<asp:TemplateField HeaderText="username" SortExpression="username">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
And here is how I'm trying to retrieve the new value, inside the RowCommand event handler of my GridView:
string userName = ((TextBox)grdUserList.Rows[rowIndex].FindControl("txtUserName")).Text;
I get the old value instead of the newly typed value when I execute this code.
Does anyone know what I am missing? Thanks in advance.
I just found out the solution for my problem. I searched and found out that the GridView is being refreshed before the retrieving process begins because I was rebinding the GridView on the Page_Load method. I fixed the problem by not rebinding the gridview when it is a post back (or at least not before I have made the changes) using the IsPostback method. Thanks for everyone's reply :)
You are retrieving new value in wrong GridView event. You have to add OnRowUpdating="grdUserList_RowUpdating" event in your GridView control and then retrieve new TextBox value.
OnRowUpdating event in code-behind:
protected void grdUserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userName = ((TextBox)grdUserList.Rows[e.RowIndex].FindControl("txtUserName")).Text;
// Write your update query and logic over here.
}
You can take a reference from here for additional knowledge.
Please let me know if you have any questions.
use this code in gridview
<Columns>
<asp:TemplateField HeaderText="SrNo">
<EditItemTemplate>
<asp:TextBox ID="txtsrno" runat="server" Text='<%#Eval("SrNo") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblsrno" runat="server" Text='<%#Eval("SrNo") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
I'm using GridView and inside that I have DropDownList
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:DropDownList ID="drop_prod" runat="server" OnSelectedIndexChanged="drop_prod_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:DropDownList ID="drop_mail" runat="server" OnSelectedIndexChanged="drop_mail_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Dropdownlist is already populated by values(dynamically) and if user will select some other value from dropdownlist I would like to get that value without postback action.
I hope you will help me.
Thanks
"I would like to get that value without postback action."
If you dont want postback at all , you can catch dropdown change event in jquery.
<asp:DropDownList class="dropdown" ID="drop_prod" runat="server" OnSelectedIndexChanged="drop_prod_SelectedIndexChanged"></asp:DropDownList>
Note class 'dropdown' above.
$(document).ready(function(){
$(".dropdown").change(function(){
var selectedValue= $(this).val();
$(".ddl-value").val(selectedValue);
});
});
EDIT:
You can put one hidden variable in page like
<input type="hidden" id="selectedvalue" runat="server" class="ddl-value"/>
on server side access it like :
selectedvalue.Value
Add a hidden field with each dropdownlist and set the hidden field value to dropdownlist value using javascript. On submit loop through hidden field and get the value.