简体   繁体   English

如何使用javascript在asp.net中的gridview中获取下拉列表的选定值?

[英]How to get selected value of dropdown within gridview in asp.net using javascript?

I have a gridview, which has multiple rows. 我有一个gridview,其中有多行。 One column is dropdownlist. 一列是下拉列表。 I need to find the selected value of dropdownlist for each row. 我需要为每一行找到dropdownlist的选定值。 When I view Page source , Its generating id StartHourDropDown for dropdown. 当我查看Page source时,其生成ID为下拉列表的StartHourDropDown。 But name for each dropdownlist is different for example grvRewardRulesDetails$ctl02$StartHourDropDown. 但是每个下拉列表的名称都不同,例如grvRewardRulesDetails $ ctl02 $ StartHourDropDown。 So how to get selected value of dropdownlist of any nth row. 那么如何获得第n行的dropdownlist的选定值。

for the dropdown in gridView you can put 对于gridView的下拉列表,您可以将

<asp:DropDownList ID="StartHourDropDown" runat="server" onchange='javascript:functionChange(this)' >

you can select from the dropdown using 您可以使用

functionChange(data)
{
    var currentDropDownValue = document.getElementById(data.id).value;
}

You can use jquery as follows 您可以按以下方式使用jquery

$("#grvRewardRulesDetails").eq(n).find("Select");

for value you can use 为了您可以使用的价值

 $("#grvRewardRulesDetails").eq(n).find("Select").val()

The above will give you the nth dropdown list. 上面将为您提供第n个下拉列表。

Here is an equation API of jQuery 这是jQuery方程式API

I like to add a CSS class to the DropDownList to make it easy to get with a jQuery selector . 我喜欢在DropDownList中添加一个CSS类,以使其易于使用jQuery选择器 This method has the advantage of not having to worry about ID's. 这种方法的优点是不必担心ID。 It does mean that you will need to have jQuery. 这确实意味着您将需要jQuery。 Most of the time websites already include it (it's included by default in the ASP.NET Web Forms New Site template). 大多数时候,网站已经包含了它(默认情况下,它包含在ASP.NET Web窗体新站点模板中)。 It's also available from NuGet if you're using Visual Studio. 如果您使用的是Visual Studio,也可以从NuGet中获得。

You'll need a reference to jQuery in the header. 您需要在标题中引用jQuery Let's load it from the Google CDN. 让我们从Google CDN加载它。 This is easy, because you don't have to download the script and place it in your site, and it's nice for the client because it will load faster due to caching. 这很容易,因为您不必下载脚本并将其放置在站点中,并且对客户端来说也很不错,因为由于缓存,它可以更快地加载。 Alternatively, go download and self-host it. 或者,下载并自我托管。

<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList runat="server" CssClass="gvddl" />
</ItemTemplate>
</asp:TemplateField>

Then in your JavaScript. 然后在您的JavaScript中。

function MyFunction()
{
    $(".gvddl") //do something with it
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net中使用javascript获取GridView中给定行的列值? - How to get Column Value for a given Row within GridView using javascript in asp.net? 如何使用带有asp.net的javascript在gridview中检查行的单元格值 - How to get checked rows cell value in a gridview using javascript with asp.net asp.net C# get selected row from gridview using javascript function - asp.net C# get selected row from gridview using javascript function 如何在asp.net中使用javascript获取选定复选框列表项的值 - How to get value of selected checkboxlist items using javascript in asp.net 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 如何使用Javascript从asp.net的下拉列表中获取选定的值? - How to get selected value from dropdownlist in asp.net using Javascript? 如何在ASP.NET C#中使用javascript从ajax组合框中获取选定的值 - how to get selected value from ajax combobox using javascript in asp.net c# 如何使用Asp.net滚动到gridview中的选定行 - how to scroll to the selected row in gridview using Asp.net 如何使用javascript从gridview下拉列表中获取选定的值 - How do you get a selected value from gridview dropdown using javascript 使用asp.net C#中的javascript从具有单选按钮的gridview列中获取值 - Get value from gridview column that has radio button using javascript in asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM