Data Pager Postback causes value lose - c#

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

Related

hide and unhide context strip menu item c#

if(e.Button == MouseButtons.Right)
{
string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
// MessageBox.Show(signatureDate);
if(signatureDate.Length > 5)
{
contextMenuStrip1.Items[0].Visible = false;
contextMenuStrip1.Items[1].Visible = true;
}else
{
contextMenuStrip1.Items[0].Visible = true;
contextMenuStrip1.Items[1].Visible = false;
}
}
I have a context strip menu that is working in my datagridview. And I selected it as Row Context Strip Menu.
What I am trying to do is to get if selected row of datagridview and control signature column is null or not. If it has signature date I want to hide or unhide "Sign" and if it doesn't have signature date hide "Unsign" item on context menu strip.
You can see in picture I enclosed.Context menu Strip
EDIT: Name of the event is MouseDown.
EDIT 2: With editing this code I can get columns data and show them on messageBox. But I can not use those data as a condition. Therefore it is not working. For example, when I select a row that is without "Signature Date" and show it on messageBox, it is working. But when I use Signature Date data as a condition It is not working. I know it is so strange and too easy to overcome but I coundn't because of that I didn't catch anything.
EDIT 3: Event
EDIT 4 (SOLVED) : I created to Context Strip Menu and specify no one of them
as Context strip Menu of Datagridview.
With Datagridview_MouseDown event, I am getting Signature Date column data and check if it is null/empty or not. If it is null/empty I specify first Context Menu strip as Context Strip Menu of Datagridview or not I do revise. I figured out the solution in this way :)
I think your problem is in the instance of context menu strip use this one see if it helps.
if(e.Button == MouseButtons.Right)
{
string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
// MessageBox.Show(signatureDate);
if(signatureDate.Length > 5)
{
dataGridView3.ContextMenu.Items[0].Visible = false;
dataGridView3.ContextMenu.Items[1].Visible = true;
}else
{
dataGridView3.ContextMenu.Items[0].Visible = true;
dataGridView3.ContextMenu.Items[1].Visible = false;
}
}
Probably your event is not firing.
Instead of using mouse down you could also use the Opening event of the contextMenuStrip
This should solve your problem
private void Form1_Load(object sender, EventArgs e)
{
dataGridView3.ContextMenu = contextMenuStrip1;
contextMenuStrip1.Opening += contextMenuStrip1_Opening;
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
// MessageBox.Show(signatureDate);
if (signatureDate.Length > 5)
{
contextMenuStrip1.Items[0].Visible = false;
contextMenuStrip1.Items[1].Visible = true;
}
else
{
contextMenuStrip1.Items[0].Visible = true;
contextMenuStrip1.Items[1].Visible = false;
}
}

Dropdown list binding issue in asp.net

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.

Accessing previous value in Hashtable during page refresh

I have a hyperlink field in a gridview. I want to store all the clicked value during a session in a hashtable for that hyperlink field.
protected partial class User : System.Web.UI.Page
{
Hashtable htable = new Hashtable();
int hTableIndex = 0;
// ...
}
In Page Load Function.
if(Session["test"] != null)`
{
for(int i = 0 ; i gvUserStatus.Rows.Count ; i++)
Hashtable tempHashTable = new Hashtable();
tempHashTable = Session["test"] as Hashtable;
if(tempHashTable.Contains(someValue))
{
//Do Something
}
}
In link click event:
protected void userID_Click(object sender, EventArgs e)
{
//logic to get the ClickedValue
if(!htable.Contains(ClickedValue))
{
htable.Add(hTableIndex++,ClickedValue);
}
Session["test"] = htable;
}
I am using the above code, but in my hash table, only the last clicked value is getting stored, what should I do to get all the previous value for an entire user session?
hTableIndex will lose its value upon postback. You will need to store it in Session as well (or just ViewState if you won't need it's value on other pages)
So you need to add this in your Page_Load event:
if (ViewState["hTableIndex"] == null)
ViewState["hTableIndex"] = hTableIndex;
and in your link click event userID_Click
protected void userID_Click(object sender, EventArgs e)
{
//logic to get the ClickedValue
if(!htable.Contains(ClickedValue))
{
var hTableIndex = Convert.ToInt32(ViewState["hTableIndex"]);
htable.Add(hTableIndex++,ClickedValue);
}
Session["test"] = htable;
}
Let me know if this works. Haven't tested it yet.
The problem is within your hTableIndex which will always point to 0 on reloading the page, you also need to put it in the session, so that next time you can get the max value of hTableIndex and add the new link at that index

C# Change text on click event

I have a field that currently changes text on a mouse click. This field is on multiple pages of my report, but clicking it only changes the text for that page. Here is my code for that:
const string sShowDetail2 = "Show Cumulative";
const string sHideDetail2 = "Hide Cumulative";
ArrayList expandedValues2 = new ArrayList();
// This function returns a value indicating whether a certain
// category's details should be expanded.
bool ShouldShowDetail2(int CategoryID)
{
return expandedValues2.Contains(CategoryID);
}
private void lbShowHide2_BeforePrint(object sender, PrintEventArgs e)
{
XRLabel label = (XRLabel)sender;
if (ShouldShowDetail2((int)label.Tag))
{
label.Text = sHideDetail2;
}
else
{
label.Text = sShowDetail2;
}
}
private void lbShowHide2_PreviewClick(object sender, PreviewMouseEventArgs e)
{
// Obtain the category's ID stored in the label's Tag property.
int index = (int)e.Brick.Value;
// Determine whether the current category's details are shown.
bool showDetail2 = ShouldShowDetail2(index);
// Toggle the visibility of the category's details.
if (showDetail2)
{
expandedValues2.Remove(index);
}
else
{
expandedValues2.Add(index);
}
Now I have another field that I need to simply toggle all instances of the field(every page) on click. I do not need it to read the tag of the particular one I clicked because it will be changing all of them at the same time. My problem is that I don't know how to get them all to change on mouse click. How could I modify the code above to change all instances of the label?
Here is something you could try
private void ChangeXRLabels(Control control)
{
foreach(Control childControl in control.Controls)
{
XRLabel label = childControl as XRLabel;
if(label != string.Empty)
label.Text = "Your Text Value goes Here";
else if(childControl.Controls.Count > 0)
ChangeXRLabels(childControl);
}
}

Text field default value overrides user input

I'm setting some default value for my text fields and those fields are in read only mode. When I pass new values to the those fields and submits the form it saved default value not the new one. I have used PostBack function but it's not working. Here is my code,
protected void Page_Load(object sender, EventArgs e)
{
if (! this.IsPostBack)
{
MakeReadControl();
}
}
private void MakeReadControl()
{
((TextBox)this.FindControl("txtMedicalCheckup")).Text = System.DateTime.Now.ToShortDateString();
((TextBox)this.FindControl("txtBosiet")).Text = System.DateTime.Now.ToShortDateString();
((TextBox)this.FindControl("txtLandSurvival")).Text = System.DateTime.Now.ToShortDateString();
((TextBox)this.FindControl("txtDefensiveDriving")).Text = System.DateTime.Now.ToShortDateString();
((DropDownList)this.FindControl("comboDrivingLicense")).SelectedIndex = 1;
((TextBox)this.FindControl("txtOtherLicense")).Text = "Not available";
}
However, the drop down list updates the value correctly.
Update:-
Instead of Read Only attribute if I use enable attribute then it works fine.

Categories