问题
I am using Sitefinity 11. I have created a Custom Widget for News Module, having Drop down for all available Categories, where I need to filter the News for Selected Category. The Control postback's but it's SelectedIndex is always ZERO.
I have checked the checkbox for "Enable View State for this page" and Page is using custom Layout Template.
The code in my .aspx file is given below:
<%@ Control Language="C#" %>
<%@ Import Namespace="Telerik.Sitefinity.Modules.News" %>
<%@ Import Namespace="Telerik.Sitefinity.Taxonomies.Model" %>
<%@ Import Namespace="Telerik.Sitefinity.Model" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.Comments" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %>
<script type="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
try
{
var ControlID = Request.Form["__EVENTTARGET"].ToString();
Control postbackControl = Page.FindControl(ControlID);
lblStatus.Text = "ControlID=" + ControlID + "<br>";
DropDownList dd = (DropDownList)postbackControl;
lblStatus.Text += "SelectedIndex=" + dd.SelectedIndex.ToString();
}
catch (Exception ex) {
lblStatus.Text += "<br>" + ex.Message;
}
}
var s = ddlCategories.SelectedIndex;
if (!IsPostBack)
{
/* List of all categories */
Telerik.Sitefinity.Taxonomies.TaxonomyManager manager1 = Telerik.Sitefinity.Taxonomies.TaxonomyManager.GetManager();
System.Collections.IList categoryList = manager1.GetTaxa<Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon>()
.Where(t => t.Taxonomy.Title == "Categories")
.Select(t =>
new { Id = t.Id, Name = t.Title.ToString() })
.OrderByDescending(t => t.Name)
.ToList();
ddlCategories.DataSource = categoryList;
ddlCategories.DataTextField = "Name";
ddlCategories.DataValueField = "Id";
ddlCategories.DataBind();
ddlCategories.Items.Insert(0, new ListItem("", ""));
}
}
protected void DdlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
Telerik.Sitefinity.Modules.News.NewsManager newsManager = Telerik.Sitefinity.Modules.News.NewsManager.GetManager();
if (this.ddlCategories.SelectedIndex > 0)
{
//this.FilterExpression += string.Format(" AND Category.Contains(({0}))", this.ddlCategories.SelectedValue.ToString());
Guid taxonFilter = new Guid(this.ddlCategories.SelectedValue.ToString());
NewsList.DataSource = newsManager.GetNewsItems().Where(newsItem => newsItem.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live
&& newsItem.GetValue<IList<Guid>>("Categories")
.Contains(taxonFilter)
).ToList();
}else {
NewsList.DataSource = newsManager.GetNewsItems().Where(newsItem => newsItem.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).ToList();}
//populate items list
//return base.GetItemsList(ref totalCount);
}
</script>
<section class="news-container">
<div class="container">
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<!-- filter panel -->
<div class="filter-panel">
<div class="panel-box categories">
<span class="label">CATEGORIES</span>
<div class="select-wrp">
<asp:DropDownList ID="ddlCategories" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DdlCategories_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</div>
</div>
</div>
</div>
<sf:SitefinityLabel id="title" runat="server" WrapperTagName="div" HideIfNoText="true" HideIfNoTextMode="Server" />
<telerik:RadListView ID="NewsList" ItemPlaceholderID="ItemsContainer" runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false">
<LayoutTemplate>
<div class="container extra-container2">
<div class="row news-post">
<asp:PlaceHolder ID="ItemsContainer" runat="server" />
</div>
</div>
</LayoutTemplate>
<ItemTemplate>
<article class="col-12 col-sm-6 col-md-6 col-lg-4 post">
<div class="holder">
<h2><sf:FieldListView ID="Title" runat="server" Text="{0}" Properties="Title" TextMode="Encode" EditableFieldType="ShortText"/> </h2>
</div>
</article>
</ItemTemplate>
</telerik:RadListView>
<sf:Pager id="pager" runat="server"></sf:Pager>
<asp:PlaceHolder ID="socialOptionsContainer" runat="server" />
</section>
EDIT: Even following code is getting the same result.
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
{
/* List of all categories */
Telerik.Sitefinity.Taxonomies.TaxonomyManager manager1 = Telerik.Sitefinity.Taxonomies.TaxonomyManager.GetManager();
System.Collections.IList categoryList = manager1.GetTaxa<Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon>()
.Where(t => t.Taxonomy.Title == "Categories")
.Select(t =>
new { Id = t.Id, Name = t.Title.ToString() })
.OrderByDescending(t => t.Name)
.ToList();
ddlCategories.DataSource = categoryList;
ddlCategories.DataTextField = "Name";
ddlCategories.DataValueField = "Id";
ddlCategories.DataBind();
ddlCategories.Items.Insert(0, new ListItem("", ""));
}
if (Page.IsPostBack)
{
try
{
var ControlID = Request.Form["__EVENTTARGET"].ToString();
Control postbackControl = Page.FindControl(ControlID);
lblStatus.Text = "ControlID=" + ControlID + "<br>";
DropDownList dd = (DropDownList)postbackControl;
lblStatus.Text += "SelectedIndex=" + dd.SelectedIndex.ToString();
}
catch (Exception ex) {
lblStatus.Text += "<br>" + ex.Message;
}
}
var s = ddlCategories.SelectedIndex;
}
please guide me what is missing?
Thanks
回答1:
Make sure the page has Enable Viewstate checked - this is in the Title and Properties of the page
回答2:
I have added a Hidden Field and I was able to get it's value after postback. Another solution was to send value in query-string.
Thanks for all replies.
来源:https://stackoverflow.com/questions/50886620/sitefinity-11-cant-get-dropdown-selected-value-on-postback-in-frontend-widget-c