I have created a dynamic matrix display using asp:table/cell combination. and added a textbox displaying a value in each cell. at this point i am creating a view state, which sto res the complete table.
Now if i update the text boxes and click on save, i get the empty table(because of postback) and also the values in ViewState are stale - they are not the updated user values.
so is there a way to create a view state on each text box change. how can i store the edited textbox values?
You do not need to store the text boxes values because they all ready posted back.
You only need to read them from the post back.
Request.Form[YourTextBox.UniqueID]
Now if you wish to save them on viewState you can use a code like
string YouData
{
set
{
ViewState["cYouData"] = value;
}
get
{
if (ViewState["cYouData"] != null)
return ViewState["cYouData"].ToString();
else
return string.Empty;
}
}
But the only reason to do that is to understand if something has change, and to find if something has change a simple hash can do the work.
Detect the PostBack and re-load the values there:
if (!Page.IsPostBack)
{
// initial load
}
else
{
// postback
}
Related
I know how to add a button to a datagridview. What I am having a problem with is changing the text displayed based on whether a associated record exists in another table. I would like to change the text on the button to "View SR" or "Add SR" depending on if a service request exists for the record. if it even possible, how I would go about this ?
You just need to capture the exact cell and cast it in a Grid View Button Cell to adjust the value displayed. Following is a tried solution:
if (SomeTrueValue)
{
((DataGridViewButtonCell)dataGridView1.Rows[0].Cells[0]).Value = "Hello";
}
else
{
((DataGridViewButtonCell)dataGridView1.Rows[0].Cells[0]).Value = "World";
}
I have a created a gridview and added a checkbox in item template. This grid has few columns along with DataKey (primary key). Due to performance gain, this grid will fetch the next set of recrods on each page change based on the page number click. So that is done.
Now when user selects a checkbox in page one and then go to page 2 and coming back to page one, then user will not see the checkbox checked as the user did earlier.
So is there a good way to persist the checkbox when user move page to page?
This checkbox be used as a flag to select the rows that can be deleted later by a button outside the grid.
Since you receive a new set each time a paging is selected, I suggest the following approach:
Create an array[] object via javascript that will add to list the datakey whenever a checkbox is selected and in turn remove it if the checkbox is deselected. Something like this:
var selectedDataKeys = [];
$('.checkboxclass').on('change', function() {
// Considering you assign the data key as id for the checkbox otherwise implement a way to retrieve the id.
var dataKey = $(this).prop('id');
// Determine if the dataKey is in the selected data keys array
var isContained = (selectedDataKeys.indexOf(dataKey) > -1 );
if($(this).is(':checked')) {
// If is contained is false - add to the array
if (!isContained)
selectedDataKeys.push(dataKey);
} else {
// If is contained is true - remove to the array
if (isContained){
selectedDataKeys = $.grep(selectedDataKeys, function(value) {
return value != dataKey;
});
}
}
});
From this point on the client user will have an active list of selected items, now its up to you to use that list to manipulate your grid display page. Either modify the display on document ready by comparing all the item on the grid display with the selectedDataKeys array or sending those keys and do the comparison server side.
Hope this helps.
I am facing issue in Gridview Check box checked and server side checkbox is showing checked = false.
Its really strange and I haven't seen yet before.
I have written following code.
<script type="text/javascript">
function SelectAll() {
if ($('.SelectAll input:checkbox').attr("checked"))
$('.chkTechs input:checkbox').attr("checked", true);
else
$('.chkTechs input:checkbox').attr("checked", false);
}
function SetCheckBoxes(item) {
//$(item).attr("target").checked // this is to find which element clicked
if ($('.chkTechs input:checkbox').filter(":not(:checked)").length > 0) {
$('.SelectAll input:checkbox').attr("checked", false)
}
else {
$('.SelectAll input:checkbox').attr("checked", true)
}
}
</script>
Server side Button Click
foreach (GridViewRow row in gvList.Rows)
{
CheckBox Checked = (CheckBox)row.FindControl("chkSelect");
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
}
The state of server controls is maintained in viewstate and changing the state of control like you are changing the checked status of checkbox with client script (javascript) is not updated in viewstate. So when you access the control on server side you do not get the changes. You have to store the changes in some hidden field and use that hidden field on server side to update your controls. It is the way asp.net implements the viewstate.
You have to get their value by checking the existence of Request.Form[xxx] parameter of the corresponding checkbox. In you case [chkSelectXXX].
1) Append something meaningful to the ID of your checkbox while creating it. Ex: the primary key val so that the ID of the checkbox should be [chkSelect_PKValue1]
2) On server side loop through Request.Form variables and check for the existence of variables that have the key value starting with chkSelect. Something like this:
foreach(var x in Request.Form)
{
if(x.StartsWith("chkSelect"))
{
//3. Then this checkbox is selected you should parse the
//PK value and do what's necessary.
}
}
I have a dropdown list that pulls data from template table. I have an Add button to insert new template. Add button will brings up jQuery popup to insert new values. There will be a save button to save the new data. On_Save_Click I enter the new data and close the popup.
Here is the proplem:
When I refresh the page, the page entering the values again. So, I get duplicate entries!
Question:
How can I avoid this issue? I check out Satckoverflow and Google, both they suggest to redirect to another page. I don't want to redirect the user to another page. How can I use the same form to avoid this issue? Please help.
You can use viewstate or session to indicate if data already inserted (button pressed).
Something like this:
private void OnbuttonAdd_click()
{
if(ViewState["DataInserted"] != "1")
{
...
// Add new entry...
...
if(data inserted successfully)
{
ViewState["DataInserted"] = "1";
}
}
}
Edit:
public bool DataInserted
{
get
{
if (HttpContext.Current.Session["DataInserted"] == null)
{
HttpContext.Current.Session["DataInserted"] = false;
}
bool? dataInserted = HttpContext.Current.Session["DataInserted"] as bool?;
return dataInserted.Value;
}
set
{
HttpContext.Current.Session["DataInserted"] = value;
}
}
...
private void OnbuttonAdd_click()
{
if(!DataInserted)
{
...
// Add new entry...
...
if(data inserted successfully)
{
DataInserted = true;
}
}
}
The simplest way is to use a post/redirect/get pattern.
Basically, the refresh action for page build with post requires to repost the data. Using this pattern, you will reload the whole page.
With ASP.Net, you have a simple alternative, use an UpdatePanel. This will refresh only part of the page using AJAX. As the page itself is still the result of a GET request, you can refresh the page. And as you use ASP.Net, it's quite easy to integrate.
Finally, you can use a home made AJAX refresh. A combination of jQuery, KnockOut and rest services (for example), can help you to avoid refreshing the full page in benefits of an ajax call.
There is some experience:
Disable Submit button on click (in client side by JavaScript).
change Session['issaved'] = true on save operation at server side and change it on new action to false.
use view state for pass parameters like RecordId (instead of QueryString) to clear on refresh page. i always pass parameter's with Session to new page, then at page load set
ViewState['aaa']=Session['aaa'] and clear Sessions.
...I hope be useful...
Do this it is very easy and effective
Intead of giving IsPostBack in the page load(),please provide inside the button click (To send or insert data)
Call the same page again after reseting all input values
protected void Btn_Reg_Click1(object sender, EventArgs e)
{
try
{
if (IsPostBack)
{
Registration_Save();
Send_Mail();
txtEmail.Text = "";
txtname.Text = "";
Response.Redirect("~/index.aspx");
}
}
catch (Exception) { }
}
You won't see any server messages after refreshing the page..
I have a label in abc.aspx, say 'label1'. I want to assign a value to 'label1' from another page xyz.ashx. How can i do this?
In general, this doesn't make sense.
When your second page is executing, the first page is gone. It simply no longer exists. There is no label for you to assign to.
Even if you could assign to the label, the previous request is over. The HTML (without the change) has already been sent to the user's browser.
I think u pls try this
call getValuein document.ready
getValue = function() {
$.post('../ax/a.ashx?val=1'
,
{
spc: data // pass some data
}
, function(data) {
document.getElementById("<%=textBox1.ClientID %>").value = data;
});
}