asp.net DropDownList inside GridView - c#

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.

Related

Set dropdownlist value in hyperlinkfield href

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

find Control in TemplateField of GridView with jquery or javascript

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.

Error updating row in asp.net GridView

I'm trying to understand why i'm getting the following error when executing an update on a GridView's row.
"No key property values were found during an update or delete operation. Check to ensure that key properties specified as binding expressions are available to the data source."
I've already setted the DataKeyNames property of the GridView.
The strange is i have 2 LinkButton in the same item template of the GridView: 1 for the update and one for the delete.. the delete one is working, the update one not.
Here is the piece of code in the FrontEnd of the page
<asp:GridView runat="server" ID="gridView" DataSourceID="listDataSource" AllowPaging="false" AllowSorting="false"
AutoGenerateColumns="false" AutoGenerateDeleteButton="false" AutoGenerateEditButton="false" AutoGenerateSelectButton="false" OnRowCommand="gridView_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="valoriFields">
<asp:DynamicEntity runat="server" Mode="Insert" />
</div>
<div class="buttonsFields buttonsFieldsEnabled">
<asp:LinkButton runat="server" ID="btnSalva" ClientIDMode="Static" CommandName="Update" CssClass="btnSaveMulti" CausesValidation="true">
<span class="icon-save"></span>
</asp:LinkButton>
<asp:LinkButton runat="server" ID="btnDelete" ClientIDMode="Static" CommandName="Delete" CssClass="btnDeleteMulti" CausesValidation="false">
<span class="icon-delete"></span>
</asp:LinkButton>
</div>
<div style="clear:both;"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<ef:EntityDataSource ID="listDataSource" runat="server" EnableInsert="false" EnableDelete="true" EnableUpdate="true" OnUpdating="listDataSource_Updating" />
And the following is the CodeBehind related to the GridView and his DataSource
listDataSource.WhereParameters.Clear();
listDataSource.EntityTypeFilter = entityType.Name;
listDataSource.ContextType = contextType;
listDataSource.EntitySetName = entityType.Name;
listDataSource.AutoGenerateWhereClause = true;
listDataSource.WhereParameters.Add(parentIdName, DbType.Int32, parentID.ToString());
gridView.SetMetaTable(table, table.GetColumnValuesFromRoute(Context));
gridView.DataKeyNames = new string[] { IdName };
ThankYou
Ok i found out you have to set and EditIndex to the GridView and so it seems to work.
Now i would like to know how it works.. because u can set the EditIndex to 0 and it doesn't matter what row you are trying to edit it works anyway.
I think when you set an EditIndex u set the state of the GridView to "Edit" or something like this..
Know i have a further question.. the first time i clic on the update button it works but after the postback if i try to click and save a different row the old error come back.
Anyone knows how it really works and how to fix this second problem?
THankYou

Pass GridView id to javascript in asp.net

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

Get gridview values from code behind

I want to get the value of a hidden field in a grid view from code-behind, but not to be used in the _RowDataBound or any other similar method. Here is my present code (it is a shopping cart scenario):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID label in the _TextChanged event. In this same event, I tried
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
but it didn't work and returns only a null value. What is the solution?
For each row in your GridView there is a HiddenField for the ProductID.
You can access the HiddenField of a row (in the example below the first row) by using the following code (assuming your HiddenField is in the first cell):
HiddenField hiddenFieldProductID =
(HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");
string productID = hiddenFieldProductID.Value
// Do something with the value
Hope, this helps.
Try to replace the HiddenField to a label or a textbox and set the visible attribute to false.
I had tried this before and it works.

Categories