将选定值设置为 UserControl 中的 DropDownList
有人可以帮助我将 DropDownList 的选定值设置为数据库给定值。我有几个 TextBox,从数据库中设置值并不难,但让我发疯的是 DropDownList。
<asp:TextBox ID="txtNaziv" runat="server" Width="430px" Text='<%# DataBinder.Eval(Container, "DataItem.Naziv") %>'></asp:TextBox>
据我所知,不可能将代码前面的所选项目值设置为 DropDownList,但我能够找到类似的内容(来自 Telerik 的 RadGrid 文档的代码片段):
protected void EmployeeDetails_DataBinding(object sender, System.EventArgs e)
{
ArrayList tocs = new ArrayList(new string[] { "Dr.", "Mr.", "Mrs.", "Ms." });
ddlTOC.DataSource = tocs;
ddlTOC.DataBind();
object tocValue = DataBinder.Eval(DataItem, "TitleOfCourtesy");
if (tocValue == DBNull.Value)
{
tocValue = "Mrs.";
}
ddlTOC.SelectedIndex = tocs.IndexOf((string)tocValue);
ddlTOC.DataSource = null;
}
问题是我使用 Linq-to-SQL,我不知道如何重新创建类似上面的代码。这就是我目前所拥有的:
protected void ddlTip_DataBinding(object sender, EventArgs e)
{
TSEntities db = new TSEntities();
var partType = (from pt in db.PartType
select new { pt.idPartType, pt.Naziv }).ToArray();
ddlTip.DataSource = partType;
ddlTip.DataTextField = "Naziv";
ddlTip.DataValueField = "idPartType";
ddlTip.DataBind();
object Tip = DataBinder.Eval(DataItem, "idPartType");
}
我还必须补充一点,这个 TextBoxes 和 DropDownList 位于 UserControl 内部,该控件在 Telerik 的 RadGrid 中用于其 EditForm。
任何帮助将不胜感激。
谢谢你!
Could someone help me with setting the selected value of the DropDownList to the database given value. I have couple of TextBoxes for which it isn't hard to set the value from the database, but what drives me crazy is DropDownList.
<asp:TextBox ID="txtNaziv" runat="server" Width="430px" Text='<%# DataBinder.Eval(Container, "DataItem.Naziv") %>'></asp:TextBox>
As far as I know, it isn't possible to set the selected item value from the code front to the DropDownList, but I was able to find out something like this (code snippet from Telerik's RadGrid documentation):
protected void EmployeeDetails_DataBinding(object sender, System.EventArgs e)
{
ArrayList tocs = new ArrayList(new string[] { "Dr.", "Mr.", "Mrs.", "Ms." });
ddlTOC.DataSource = tocs;
ddlTOC.DataBind();
object tocValue = DataBinder.Eval(DataItem, "TitleOfCourtesy");
if (tocValue == DBNull.Value)
{
tocValue = "Mrs.";
}
ddlTOC.SelectedIndex = tocs.IndexOf((string)tocValue);
ddlTOC.DataSource = null;
}
The problem is I'm using Linq-to-SQL and I'm not sure how to recreate something like the above code. This is what I currently have:
protected void ddlTip_DataBinding(object sender, EventArgs e)
{
TSEntities db = new TSEntities();
var partType = (from pt in db.PartType
select new { pt.idPartType, pt.Naziv }).ToArray();
ddlTip.DataSource = partType;
ddlTip.DataTextField = "Naziv";
ddlTip.DataValueField = "idPartType";
ddlTip.DataBind();
object Tip = DataBinder.Eval(DataItem, "idPartType");
}
One more thing I have to add that this TextBoxes and DropDownList are inside the UserControl which is being used inside Telerik's RadGrid for its EditForm.
Any help would be appreciated.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要设置下拉列表的 SelectedValue:
您也可以这样做:
编辑:
包含将列表直接绑定到 db.PartType 的代码:
You need to set the SelectedValue of the dropdown:
You can also do it like this:
EDIT:
Included code to bind list directly to db.PartType:
试试这个
Try this one