I have a placeholder which has some dropdownlist and a button, now this placeholder is initially hidden and when user makes any search on the page, based on that the placeholder gets visible and the dropdownlist gets filled, this is working absolutely fineNow the problem starts when i click on the button inside the placeholder (next to dropdownlist), at this point of time, all the dropdownlist gets blank. I understand first page load executes and because there are no bindings (as dropdowns are binded on search click) it would make it blank, but i am not getting once the dropdown has been binded why it would matter for the page load to unbind it. Here's my code:
Page Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
plcView.Visible = false;
}
}
Search Button Click:
protected void btnSearch_Click(object sender, EventArgs e)
{
try
{
if (!AppGlobal.IsSanadCompleted(AppGlobal.Sanads.Amma, txtITSId.Text))
{
lblErrorMessage.CssClass = "dnnFormMessage dnnFormWarning";
lblErrorMessage.Text = "You need Amma Sanad to attempt for Hifz Program.";
plcView.Visible = false;
}
else
{
plcView.Visible = true;
txtJuz.Text = (Hifz.GetLastJuzAttempted(txtITSId.Text) + 1).ToString();
drpAyahFrom.DataTextField = "Key";
drpAyahFrom.DataValueField = "Value";
drpAyahFrom.DataSource = objHifz.GetAyahListForHifzProgram(Convert.ToInt32(txtJuz.Text));
drpAyahFrom.DataBind();
drpAyahTo.DataTextField = "Key";
drpAyahTo.DataValueField = "Value";
drpAyahTo.DataSource = objHifz.GetAyahListForHifzProgram(Convert.ToInt32(txtJuz.Text));
drpAyahTo.DataBind();
drpGrade.DataSource = AppGlobal.GetGrades();
drpGrade.DataBind();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
I even tried enabling EnableViewstate at page and also at skin (I am using Dotnetnuke) level, but still it does not makes any difference.
Can anyone please suggest where i would be getting wrong.
DotNetNuke is nothing but an ASP.Net app. So to look into your issue we have to delve into the Page Life-Cycle of an ASP.Net page. There you will notice a Page_Event called SaveStateComplete. You see, this is called just before the Render event(At this event the page is displayed to the end user). So any changes done after rendering the page will not be saved in to the ViewState by default. Since your DropDowns are blank during SaveStateComplete, after a PostBack you're getting empty DropDowns.
The solution is to forcefully save the DataSource in your custom ViewState. For ex. in your button click, add the following:
var obj = objHifz.GetAyahListForHifzProgram(Convert.ToInt32(txtJuz.Text)));
drpAyahFrom.DataSource = obj;
ViewState["myUniqueKey"] = obj;
Then at the Page_Load event, you can use:
if (!IsPostBack)
{
plcView.Visible = false;
}
else
{
if(ViewState["myUniqueKey"] != null)
{
drpAyahFrom.DataTextField = "Key";
drpAyahFrom.DataValueField = "Value";
drpAyahFrom.DataSource = ViewState["myUniqueKey"];
drpAyahFrom.DataBind();
}
}
You may have to perform the casting as per the requirement.
Related
within my code , after a research via a Formview , I need to call the listview.databind and this makes impossible to get the Formview data , even if in the screen they still appear .
this is my code
protected void DemandeSearchFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
{
ListView listview = (ListView)panelPagination.FindControl("listdeclarations");
ViewState["search"] = "search";
listview.DataBind();
}
the databind() normally call this method
public DeclarationGeneraleBean RechercheByCritere()
{
DeclarationGeneraleBean declarationBean = new
DeclarationGeneraleBean();
declarationBean.IdService = (int) Session["idService"];
if (ViewState["search"] != null)
{
TextBox numOrdre =
(TextBox)DemandeSearchFormView.FindControl("numtxt");
}
the ViewState["search"] is null , I dont know why ?? it seems that the databind() recharge the page or something like this .
Have any one an idea how to deal with this ?
Do you set the viewstate in your page load event?
If yes, i think you should add a condition in your Page_Load event :
private void Page_Load()
{
if (!IsPostBack)
{
}
}
it prevent the data to be reloaded on this event, if a post is submited.
I am new to asp.net. Here is the scenario. I am trying to build a Search functionality. If I enter the value in search box i.e. "Test" and click search icon SQL Server returns results. I have limited datapage size = "1". When I click on next page it refreshes the page and my search box looses the value I entered in this case "Test". If no value is passed SQL Server returns a default result so everytime I navigate through pages it works only for first page each click after that returns me default value. I bind list view on PreRender of datapage. Here are the code snippets.
protected void search_ServerClick(object sender, EventArgs e)
{
mydatapager_PreRender(sender, e);
}
protected void mydatapager_PreRender(object sender, EventArgs e)
{
string var_search_firstname = globalsearchinput.Value.ToString();
string var_search_city = citysearchinput.Value.ToString();
string var_search_state = statesearchinput.Value.ToString();
bool isadvancedsearch = false;
//Determine whether it's advanced search or not.
string MethodCaller = "";
//MethodCaller = ((HtmlButton)sender).ID;
//if (MethodCaller == "search")
//{
// isadvancedsearch = false;
//}
//else
//{
// isadvancedsearch = true;
//}
Utility utl = new Utility();
friendrequestsentnotificationpanel.Visible = false;
listview1.DataSource = utl.SearchProfile(var_search_firstname, var_search_city, var_search_state, isadvancedsearch);
listview1.DataBind();
}
Add ispostback==false condition in preRender. It helps to retain the serch string
Previously when my RadGrid was not a batch edit grid I was able to use the grid's AddNewRecord button to redirect the user to another page with the following code:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "InitInsert")
{
Response.Redirect(redirectUrl + "?ProductID=" + this.ProductId);
}
}
After I made my grid a batch edit grid the Add New Button doesn't go into the ItemCommand event anymore and instead tries adding an inline insert record row to the grid. Is there anyway I can still use this button and override its functionality to still redirect the user?
So I've tested this and confirmed what I suspected in the comments. When EditMode="Batch", the "Add New Record" button, along with others, no longer cause a postback. You can override this by removing the JavaScript of the OnClientClick in the RadGrid1_ItemCreated like so:
Add this to your RadGrid1 attributes:
OnItemCreated="RadGrid1_ItemCreated"
Code behind (note: there is actually a Button AND a LinkButton):
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item.ItemType == Telerik.Web.UI.GridItemType.CommandItem) {
//This is the icon with the plus (+) sign unless you've changed the icon
Button iconButton = e.Item.FindControl("AddNewRecordButton");
if (iconButton != null) {
iconButton.OnClientClick = "";
}
//This is the words "Add New Record" or whatever you've called it
LinkButton wordButton = e.Item.FindControl("InitInsertButton");
if (wordButton != null) {
wordButton.OnClientClick = "";
}
}
}
This should allow the postback to happen and the code you posted should be able to run.
After a long time searching for this problem I've decided to try my luck here... I'm trying to add a tooltip (title) for the items in a Drop Down List.The title is showed in IE and Firefox browsers. The DDL is creating dynamically and the DDL's DataSource is a list called "labCourses" .This is my code:
protected void Page_Load(object sender, EventArgs e)
{
ExpScheduleClass ExpSC = new ExpScheduleClass();
List<string> labCourses = ExpSC.getCourseList();
// dynamically create a dropdown list (aka DDL)
DDLCourses = new DropDownList();
DDLCourses.DataSource = labCourses; // set the data source
DDLCourses.DataBind(); // bind the data to the DDL control
BindTooltip(DDLCourses);
DDLCourses.AutoPostBack = true;
DDLCourses.SelectedIndexChanged += DDLCourses_SelectedIndexChanged;
coursePH.Controls.Add(DDLCourses);
}
public void BindTooltip(ListControl lc)
{
for (int i = 0; i < lc.Items.Count; i++)
{
lc.Items[i].Attributes.Add("title", lc.Items[i].Text);
}
}
I looked at the option element with the web inspector and the title was there (in the html), but is does not shown when the mouse was over the option.
In other browsers the title is shown on mouse hover. What may be the cause for this? Thank you very much...
I've got a GridView control and Combobox control that are both successfully populated in my Page_Load event (within a block that checks for IsPostBack == false).
I've got an empty button 'btnClick' event handler which will reload the page when clicked. Both the GridView and Combobox controls have their EnableViewState property set to True. The behaviour I was expecting and hoping for was:
Page will reload with the GridView control still populated.
Page will reload with Combobox still populated and the item selected by the user still set as the selected item.
Unfortunately, the behaviour I'm getting is as follows:
GridView control is now empty and shows no data.
Combobox is now empty.
Code as follows:
public MyPage()
{
this.Load += new EventHandler(Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
DataAccessObj daObj = new DataAccessObj();
foreach (DataRow dataRow in daObj.GetAllData())
{
ListItem listItem = new ListItem(dataRow.ToString(), dataRow.Id.ToString());
myCombobox.Items.Add(listItem);
}
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Do nothing
}
What I would like to do is allow the user to select an item from the Combobox. Upon clicking Submit, the GridView would be repopulated (based upon the selected item). The Combobox will remain populated and show the last selected item.
Can someone help explain where I might be going wrong? TIA
When you click your button, the page is posted back, in your page load, if it is a postback you need to databind the grid appropriately you need to add a condition to your page load event like
Firstly on your btn_click, you need to store the id selected with something like:
if (myCombobox.SelectedItem != null)
{
if (int.TryParse(myCombobox.SelectedItem.Value, out reportedById) == false)
{
reportedById = 0;
ViewState["reportedById"] = reportedById; // Need to remember which one was selected
}
}
Then On your Post Back
else (IsPostBack)
{
if (ViewState["reportedById"]) != null)
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(Convert.ToInt32(ViewState["reportedById"]));
IncidentGrid.DataBind();
myCombobox.SelectedItem.Value = ViewState["reportedById"].ToString(); // set combo
}
else
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}