首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用在同一GridView RSS中的radcombobox的Onclientsideselectedindexchanged上触发的javascript函数在GridView中查找textbox

如何使用在同一GridView RSS中的radcombobox的Onclientsideselectedindexchanged上触发的javascript函数在GridView中查找textbox
EN

Stack Overflow用户
提问于 2012-05-17 04:09:30
回答 1查看 12.8K关注 0票数 1

我有两个控件,一个是TextBox,另一个是radcombobox in a ItemTemplate of a GridView。我想要做的是当radcombobox的event onclientsideselectedindexchanges被触发时。我想用JavaScript在客户端显示/隐藏TextBox。

这样做的基本目的是避免回发以显示将被DataBound到数据库的TextBox。

如果不可能在客户端做,那么请在服务器端建议一些替代方案。

EN

回答 1

Stack Overflow用户

发布于 2012-05-17 17:49:27

在我的示例中,我有一个带有模板字段的GridView,它包含一个DropDown菜单和一个TextBox。JavaScript中的OnLoad事件将TextBox的显示样式设置为"none"。设置GridView的DropDown in OnRowDataBound事件的OnChange事件,并调用JavaScript函数,将TextBox显示样式设置为您需要的样式。

在我的示例中,仅当DropDown选择的索引为"1"时才显示TextBox。

我已经在我的代码中完成了所有这些-参考这个: Gridview ID="GridView1"

代码语言:javascript
复制
<Columns>
    <asp:TemplateField HeaderText="Order Status">
        <ItemTemplate>
           <asp:DropDownList ID="ddlOrderStatus" runat="server" Width="104px" ToolTip="Select order status">
            </asp:DropDownList><br />
             <asp:TextBox ID="txtShipTrackNo" runat="server" 
      Width="100px" Text='<%# Eval("sct_order_docket_id") %>'></asp:TextBox>
</ItemTemplate>
    </asp:TemplateField>
</Columns>

代码语言:javascript
复制
<script type="text/javascript">
var TargetBaseControl = null;
window.onload = function() {
    try {
    //get target base control.
    TargetBaseControl = document.getElementById('<%= this.GridView1.ClientID %>');
    for (var rowId = 1; rowId < TargetBaseControl.rows.length; ++rowId) {
    var txtShip = TargetBaseControl.rows[rowId].cells[0].getElementsByTagName("input")[0];
    txtShip.style.display = "none";
        }
    }
    catch (err) {
        TargetBaseControl = null;
    }
}

function selectCheck(rowid) 
{
     TargetBaseControl = document.getElementById('<%= this.GridView1.ClientID %>');
     var ddlStatus = TargetBaseControl.rows[rowid].cells[0].getElementsByTagName("select")[0];
     var txtShip = TargetBaseControl.rows[rowid].cells[0].getElementsByTagName("input")[0];
     txtShip.style.display = "none";
     if (ddlStatus.selectedIndex == '1') {
         txtShip.style.display = "block";
     }         
}

代码语言:javascript
复制
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlOrderStatus");
        string strFunc = string.Format("return selectCheck('{0}')", e.Row.RowIndex + 1);  
        ddlStatus.Attributes.Add("onchange", strFunc);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10625948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档