3

我有两个下拉列表 ddlCountry 和 ddlState 以及一个按钮提交。
虽然 ddlstate 在更新面板中。
在提交数据库中两个下拉存储的按钮值时。
我在中继器控件中显示数据。(在 HTML 表结构中)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 (SelectRow(this)) 函数在 javascript 中突出显示转发器值和下拉值匹配并被选中。

<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。

从 Javascript 我正在触发 __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

pageLoad上的绑定国家

 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 中选择状态。

4

2 回答 2

5

如果您调试代码,您将看到 __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>

湾。选择国家后更改您的 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;
于 2012-06-16T04:23:55.867 回答
-1

这可能是因为您没有为 Text = "India" 的下拉列表绑定单独的值,因此服务器无法识别该值已更改,因此未处理 selectedindexchanged 事件。

尝试创建一个对印度文本具有不同价值的数据源,或者只使用一个印度文本

我可以清楚地看到印度的 listview 中有两个重复的记录,带有重复的键 (2)

不建议这样做。只使用一个应该具有唯一键的记录。

我可以理解您只有一个数据源,因此以这样一种方式修改您的代码,即在向 ddlCountry 添加项目时检查该项目是否已经存在或是否存在,只是不要再次添加它。替换 Bindcontry 代码中的后续代码,它应该可以正常工作。

    foreach(DataRow dr in dataTableCountry.Rows)
    {
        countryID = Convert.ToInt32(dr["countryID"]);
        Country= dr["Country"].ToString();

        if ( ! dictioneryCountry.Contains(countryID))
        {
           dictioneryCountry.Add(countryID,Country);
        }
    }

让我知道它是否有效

于 2012-05-30T10:18:18.133 回答