简体   繁体   English

无法将数据库数据显示为SelectedValue asp.net C#

[英]Unable to show database data as SelectedValue asp.net c#

I am trying to retrieve data out from database and set them as SelectedValue in dropdownlist and radiobuttonlist . 我正在尝试从数据库中检索数据并将它们设置为dropdownlist和radiobuttonlist中的SelectedValue

The thing is, the data is being retrieved and can be shown when i set as text in label but unable to show in dropdownlist and radiobuttonlist. 关键是,当我在标签中设置为文本但无法在下拉列表和单选按钮列表中显示时,数据将被检索并可以显示。 The coding looks fine to me but nothing is being shown. 编码对我来说似乎不错,但是什么也没显示。 So i need help to see what when wrong? 因此,我需要帮助以查看发生错误时的情况?

texttime and txttime1 can display result but only ddlEditEventStatus,ddlAddMembershiplist2 and RadioButtonList1. texttime和txttime1可以显示结果,但只能显示ddlEditEventStatus,ddlAddMembershiplist2和RadioButtonList1。

CodeBehind: 代码背后:

 this.dvUpdate.Visible = true;
                //force databinding
                DataSet ds = dal.getEditEventDetails(promoID);
                this.dvUpdate.DataSource = ds;
                this.dvUpdate.DataBind();

                this.mdlPopup2.Show();

                populatePanelDDL4UpdateEvent(ds.Tables[0].Rows[0]["membershipType"].ToString());

                //remember the session of the old name
                Session["oldTitle"] = ds.Tables[0].Rows[0]["titlePromo"].ToString();

                ((RadioButtonList)dvUpdate.FindControl("RadioButtonList1")).SelectedValue = ds.Tables[0].Rows[0]["defaults"].ToString();

                //set eventCancelledStatus
                //set the time
                //((Label)dvUpdate.FindControl("Label3")).Text = ds.Tables[0].Rows[0]["defaults"].ToString();;
                ((DropDownList)dvUpdate.FindControl("ddlEditEventStatus")).SelectedValue = ds.Tables[0].Rows[0]["promoStatus"].ToString();
                ((DropDownList)dvUpdate.FindControl("ddlAddMembershiplist2")).SelectedValue = ds.Tables[0].Rows[0]["membershipType"].ToString();

                ((DropDownList)dvUpdate.FindControl("txttime")).Text = ds.Tables[0].Rows[0]["startTime"].ToString();
                ((DropDownList)dvUpdate.FindControl("txttime1")).Text = ds.Tables[0].Rows[0]["endTime"].ToString();

Html: HTML:

  <td class="fields">
                                <asp:DropDownList ID="ddlEditEventStatus" CssClass="selectMidStyle"  Width="255px" runat="server">
                                    <asp:ListItem Text="Opened" Value="Opened"></asp:ListItem>
                                    <asp:ListItem Text="Closed" Value="Closed"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                            </tr>
                            <tr>
                                <td class="text">
                                    <asp:Label ID="add_membershipList2" runat="server" Text=" Membership Type : "></asp:Label>
                                </td>
                                <td colspan="3">
                                    <asp:DropDownList ID="ddlAddMembershiplist2" runat="server" Width="255px"  CssClass="selectMidStyle">

                                    </asp:DropDownList>

                                </td>
                            </tr>
                              <tr>
                                <td class="text">
                                    </br>
                                    <asp:Label ID="Label3" runat="server" Text=" Default Promotion : "></asp:Label>
                                </td>

                                <td>
                                    </br>  
                                 <asp:RadioButtonList ID="RadioButtonList1" runat="server"  ValidationGroup="EditValidationGp">
                                     <asp:ListItem Value="Y">Yes</asp:ListItem>
                                     <asp:ListItem Value="N">No</asp:ListItem>
                                 </asp:RadioButtonList>


                                </td>

the problem is you are not finding the required dropdownlist item correctly. 问题是您没有正确找到所需的下拉列表项。 Use findbyvalue method like this: 使用如下的findbyvalue方法:

 //This is how you set selecteditem
((RadioButtonList)dvUpdate.FindControl("RadioButtonList1")).Items.FindByValue( ds.Tables[0].Rows[0]["defaults"].ToString()).Selected=true; 

Use the same for the other dropdownlists 将其用于其他下拉列表

I figure it out that there are some differences in the database value compared to the values in dropdownlist and radiobuttonlist. 我发现与dropdownlist和radiobuttonlist中的值相比,数据库值存在一些差异。 Instead nchar, i should set it to nvarchar. 取而代之的是nchar,我应该将其设置为nvarchar。 Thank you for the comments. 谢谢你的意见。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM