ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-当下拉索引selectedIndex相同时,不会触发__doPostBack()

2019-12-01 08:35:28  阅读:285  来源: 互联网

标签:c-4-0 dopostback asp-net javascript


我有两个下拉菜单ddlCountry和ddlState和一个按钮Submit.
当ddlstate在更新面板中时.
在提交两个下拉按钮的值时,将它们存储在数据库中.
然后在Repeater Control中显示数据.(以HTML Table结构)
ASPX代码

<table id="tablelist" class="csstablelist" cellspacing="1" cellpadding="1">
                    <tr>
                        <td class="csstablelisttoptr">
                            ddlCountryID
                        </td>
                        <td class="csstablelisttoptr">
                            ddlCountryText
                        </td>
                        <td class="csstablelisttoptr">
                            ddlstateText
                        </td>
                    </tr>
                    <asp:Repeater ID="repeaterList" runat="server" OnItemDataBound="repeaterList_ItemDataBound">
                        <ItemTemplate>
                            <tr onclick="selectRow(this);">
                                <td class="csstablelisttd" style="display: none">
                                    <asp:Label ID="ddlCountryID" runat="server" Text='<%#Eval("ddlCountryID")%>'></asp:Label>
                                </td>
                                <td class="csstablelisttd">
                                    <asp:Label ID="ddlCountryText" runat="server" Text='<%#Eval("ddlCountryText")%>'></asp:Label>
                                </td>
                                <td class="csstablelisttd">
                                    <asp:Label ID="ddlstateText" runat="server" Text='<%#Eval("ddlstateText")%>'></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </table>
<asp:DropDownList ID="ddlCountry" runat="server" CssClass="csstextbox" Width="207px" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
</asp:DropDownList>
<asp:UpdatePanel ID="updatePanelState" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="ddlState " runat="server" CssClass="csstextbox" Width="177px">
        </asp:DropDownList>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button ID="btnSave" runat="server" Width="80px" OnClientClick="return validateForm();" Text="Save" CssClass="cssbutton" OnClick="btnSave_Click" />


ddlCountryID | ddlCountryText | ddlstateText
1            | USA            | XYZ
2            |India           | PQR

TR的Onclick我在javascript中编写了(SelectRow(this))函数,以突出显示转发器值和下拉值匹配并被选中.

<script type="text/javascript">
function selectRow(objTR)
{
 var ddlCountry =document.getElementById('<%=ddlCountry.ClientID %>');
 var ddlState =document.getElementById('<%=ddlState .ClientID %>');

    for (i = 0; i < ddlCountry .options.length; i++)
    {
        if (ddlCountry .options[i].text == objTR.cells[1].innerText.trim())
            break;
    }           
    ddlCountry .options[i].selected = true;

    __doPostBack(ddlCountry .id, objTR.cells[2].innerText.trim());    
}
</script>

我在代码后面写了ddlCountry SelectedIndexChangedEvent.

从Java语言中,我正在触发__doPostBack()并将ddlCountry作为事件目标ddlStateText作为事件参数传递给SelectedIndexChangedEvent,并在这样的事件中获取值.

string stateDescription = Request["__EVENTARGUMENT"];
ddlState .Items.FindByText(stateDescription ).Selected = true;//for highliting the repeater value and dropdown value match and selected

在页面上绑定国家

 if(!Ispostback)
protected Void BindCountry()

{

strSQL = @"SELECT countryID,Country from Country_Master";
        DataTable dataTableCountry = null;
        dataTableCountry = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];

        int countryID;
                string Country;     
        var dictioneryCountry = new Dictionary<int, string>();
        foreach(DataRow dr in dataTableCountry.Rows)
        {
            countryID = Convert.ToInt32(dr["countryID"]);
            Country= dr["Country"].ToString();          
            dictioneryCountry.Add(countryID,Country);
        }
        ddlCountry.Items.Clear();
        ddlCountry.DataTextField = "Value";
        ddlCountry.DataValueField = "Key";
        ddlCountry.DataSource = dictioneryCountry;
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("[Select]", "-1"));
        ddlCountry.Items[0].Selected = true;
}

我的问题是,如果我有以下中继器数据.

ddlCountryID | ddlCountryText | ddlstateText
1            | USA            | XYZ
2            |India           | PQR
2            |India           | MNO

当我选择具有国家印度和州mno的行号3时,__ dopostback()方法将启动.

当我转到第1行时,则__dopostback()方法启动.

当我从第1行到第3行时,方法是正确的射击方式,但是当从第3行到第2行具有国家ID相同的__dopostback()方法时,则不启动,并且未从ddlstate中选择状态.

解决方法:

如果调试代码,则会看到__doPostBack()确实在工作,因为正在执行Page_Load中的代码(尝试在此处设置断点以进行检查).问题是DropDownList的索引没有更改,因此不会触发IndexChanged事件,并且ddlCountry_SelectedIndexChanged永远不会运行.当您调用__doPostBack()时,这与从背后的代码中手动调用ddlCountry_SelectedIndexChanged不同.

除了设置此下拉列表值之外,您是否有其他原因需要回发?

解决方案1

我建议删除__doPostBack()行并添加以下内容:

var state = objTR.cells[2].innerText.trim();
for (var i = 0; i < ddlState.options.length; i++)
{
    if (ddlState.options[i].text == state)
    {
        ddlState.options[i].selected = true;
        break;
    }
}

解决方案2

另外,如果您确实需要发回邮件,则可以执行以下操作:

一种.在更新面板中添加一个隐藏按钮,如下所示:

<div style="display: none">
<asp:Button ID="btnState" OnClick="btnState_Click" runat="server" />
</div>

b.选择国家/地区后,将您的JavaScript更改为执行以下操作:

document.getElementById('__EVENTARGUMENT').value = objTR.cells[2].innerText.trim();
document.getElementById('<%= btnState.ClientID %>').click();

C.创建一个FilterState()函数并从您的ddlcountry indexchanged事件中调用它

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    FilterState();
}

private void FilterState()
{
// insert whatever filtering code you had here
}

d.添加您的btnState_Click函数(请注意,如果您之前已设置了ddlState的选定项,则如果已选择该项,则会抛出错误.):

protected void btnState_Click(object sender, EventArgs e)
{
    FilterState();
    string state = Request["__EVENTARGUMENT"];
    if (state != "")
        ddlState.SelectedValue = state;
}

解决方案3

第三种解决方案将允许您仍然使用__doPostBack,并将此事件放入Page_Load函数中,从而完全废弃SelectedIndexChanged事件:

if (IsPostBack && Request["__EVENTTARGET"] == ddlCountry.UniqueID)
{
    FilterState();
    string stateDescription = Request["__EVENTARGUMENT"];
    if (stateDescription != "")
        ddlState.SelectedValue = stateDescription;
}

另外,对于BindCountry函数,没有使用字典的真正理由,您可以直接绑定DataTable:

strSQL = @"SELECT countryID,Country from Country_Master";
DataTable dataTableCountry = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];
ddlCountry.Items.Clear();
ddlCountry.DataSource = dataTableCountry;
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "countryID";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("[Select]", "-1"));
ddlCountry.Items[0].Selected = true;

标签:c-4-0,dopostback,asp-net,javascript
来源: https://codeday.me/bug/20191201/2079551.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有