Here's my issue, I'm using visual studios 2010 and I'm trying to create a form to fill then send it. This form requires a fileupload and some informations. The problem is that when I want to show/hide some pannel or textbox I can't do it dynamicly and I have to use autopost back which unselect the file selected in the fileupload control :
protected void CB_image_CheckedChanged(object sender, EventArgs e)
{
if (CB_image.Checked)
{
PanelImage.Visible = true;
}
else
{
PanelImage.Visible = false;
}
}
This is an exemple of code i'd like to pull out without using autopostback property on my CB_image (CB = checkbox). Any help would be appreciated, thanks
You can't do that. Something has to trigger a postback so the code on the server-side can check whether the checkbox is checked or not, and act according to it.
You can use a javascript timer that initiates a postback and work that way, I wouldn't do that though.
btw,
protected void CB_image_CheckedChanged(object sender, EventArgs e)
{
PanelImage.Visible = CB_image.Checked;
}
looks much cleaner!
Related
I am trying to add a basic switch to my site in order to switch between static and responsive layouts.
I have two linkbuttons at the bottom of my page:
<div id="toggleView">
<asp:linkbutton ID="lbtnMobile" runat="server" Visible="false">Switch to Mobile site</asp:linkbutton>
<asp:linkbutton ID="lbtnFull" runat="server" >Switch to Full site</asp:linkbutton>
</div>
They both have a very similar OnClick event.
protected void lbtnFull_Click(object sender, EventArgs e)
{
c.ViewChange = true;
Session["Customer"] = c;
}
protected void lbtnMobile_Click(object sender, EventArgs e)
{
c.ViewChange = false;
Session["Customer"] = c;
}
The events should set a boolean in a class file (User.vb) between true or false and then save the session, on postback the Page_Load event is supposed to read this boolean and use it to adjust the Viewport meta tag:
protected void Page_Load(object sender, System.EventArgs e)
{
//Other Stuff in here, irrelevant to current question
HtmlMeta view = new HtmlMeta();
view.Name = "viewport";
if (c.ViewChange = false)
{
view.Content = "width=device-width, initial-scale=1";
lbtnFull.Visible = true;
lbtnMobile.Visible = false;
}
else
{
view.Content = "width=1040px, initial-scale=1";
lbtnFull.Visible = false;
lbtnMobile.Visible = true;
}
MetaPlaceHolder.Controls.Add(view);
}
However, when I click on the "Switch to Full Site" linkbutton, the page will postback but nothing will have changed. Does the postback get triggered too early somehow?
The page load event will happen BEFORE your click event. Reference this here.
This means your check for the ViewChange will happen before you set it in the OnClick handler.
You should change
if (c.ViewChange = false)
to
if (c.ViewChange == false)
for something to happen. But I think it won't be what you expect. Because page_load is executed before click event. You may move some code from page_load to click event handlers.
When ever you postback the Page_Load always get called. So, the code mentioned inside Page_Load would always get executed.
protected void Page_Load(object sender, System.EventArgs e)
{
... All your mentioned code will be executed.
}
Therefore, you won't find any change in your HTML page currently viewed in a browser because at postback initial content also got executed. You need to wrap your content inside !IsPostBack to make it work properly.
Thus, modify you code in following way.
protected void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostback)
{
... All your mentioned code will be executed during normal load.
}
}
Also, you need to add some extra code in LinkButton click event i.e. what to show and what to hide.
Firstly your implementation in the Page_Load isn't very clear.
Nevertheless this is what I recommend, from what I've understod:
As the page load will get executed before the post-back event like the buton or link click, you need persist the value of the class object
Make a protected property of type of your class (where you store/manage the ViewChange atribute)
The property should internally (in the get & set), hold/persist the value in session/viewstate (similar to what you've written)
The setting and reading should only be by referring the property directly (and not how you've done the click-event)
On clicking of the button and post setting the new value, you will have to redirect to the same page, as only then the Page_Load event will get the new boolean value that you've just changed in the click-event; (Page_Load occurs befoer the any other post-back event)
An alternative to the fresh redirection is that, you could make a function that has the view changing logic (as depicted in your Page_Load code), and this function should be called on your button/link click event (post boolean value change) and also in the Page_Load event, but within the "!IsPostBack" block
Hope this helps you.
This is the Error:
Collection was modified; enumeration operation may not execute.
This error is found when for each process tries to find the items.
protected void SubmitButton_Click(object sender, EventArgs e)
{
foreach (DataListItem item in this.ImageRepeater.Items)
{
FileUpload fup = (FileUpload)ImageRepeater.FindControl("ImageUpload");
if (fup.HasFile)
{
updateImageChanges();
divTopImageCheckChangedmessage.Visible = false;
}
}
}
My requirement is that I want to satisfy the check if there is no file loaded into Fileupload control within ASP.Net DataList then not to allow, the updateImageChanges(); function to be hit.
I will be grateful to you all.
Not sure why you are looping through the DataList Items ,I think your code may look something like this :
Check this sample code for
protected void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
DataListItem di = btn.NamingContainer as DataListItem;
FileUpload fu = di.FindControl("fu") as FileUpload;
if (fu.HasFile)
{
// save to the database :
}
}
I think this error is your updateImageData call method gave
Since updateImageChanges() doesn't take a parameter, I assume it iterates through your repeater items again? How does it know what item to work with? Whatever your are doing in that function (since you did not post the code for it) is the source of your error, it is not in the code you posted. However, this solution will work for you:
You can update that function to take a parameter (say a FileUpload control) and then pass that from your if statement to the updateImageChanges() method. That way this method will ONLY update changes, and not need to loop through your repeater items again to get what it needs.
Something like this:
protected void SubmitButton_Click(object sender, EventArgs e)
{
foreach (DataListItem item in this.ImageRepeater.Items)
{
FileUpload fup = (FileUpload)ImageRepeater.FindControl("ImageUpload");
if (fup.HasFile)
{
updateImageChanges(fup);
divTopImageCheckChangedmessage.Visible = false;
}
}
}
private void updateImageChanges(FileUpload fup)
{
// remove your code that loops through to get to the correct file upload
// leave code that works with current fup and use passed parameter instead of the repeater items collection
}
I have a main page with a Gridview that shows data from a Database. In the gridview there is a button, when this button is clicked a Dialog will appear allowing u to edit the selected row. The dialog is created as an aspx class. When i edit the data and close down the dialog i want to refresh my GridView on the Main page so the edited data is represented. How can i do this?
my code for editing the data and closing down the dialog is this:
protected void editButton_Click(object sender, EventArgs e)
{
string alias = Request.QueryString["alias"];
string custid = Request.QueryString["custid"];
controller.EditDeliveries(custid, alias, tdaysField.Text, thoursField.Text, ttypeField.Text, pdaysField.Text, phoursField.Text, ptypeField.Text);
ClientScript.RegisterClientScriptBlock(GetType(), "onUpload", "<script type='text/javascript'> window.close();</script>");
}
Can anyone help ?
And if u need to see more code please tell me.
Just set your datasource again and rebind it in the postback
gvMyGrid.DataSource = myData; //Fresh from the database
gvMyGrid.DataBind(); //Bam, fresh data
Edit: Oh and if it's another Control that is the source of the postback, you can use a Bubble Event to trigger the refresh.
Second Edit: To have the Dialog Page tell the Main Page to update that grid:
RaiseBubbleEvent(this, new CommandEventArgs("DataUpdated", "This could be null, or I could be a message to let the user know things are updated"));
Then on your main Page
protected override bool OnBubbleEvent(object sender, EventArgs e)
{
if (e is CommandEventArgs)
{
var args = (CommandEventArgs)e;
//Could use args.CommandArgument here
switch(args.CommandName)
{
case "DataUpdated":
gvMyGrid.DataSource = myData;
gvMyGrid.DataBind();
return true; //Handled the event LIKE A BOSS
}
}
return false; //I didn't handle this event
}
I want to minimize the amount of code i have to write for this small problem. I have 1 textbox that has a relationship with 2 checkboxes as yes and no. The textbox on form load is set to disabled. When the yes checkbox is changed this event occurs -
private void checkYes1_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = true;
checkNo1_cbx.Checked = false;
}
and when the no checkbox is changed -
private void checkNo1_cbx_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = false;
checkYes1_cbx.Checked = false;
}
Although another problem is that i have to press yes twice to get it to check.
This is for a question on a form and so far it goes up to 11 questions and more will be added in the future. So my 2 problems so far is -
How can I fix the problem when the checkbox is changed I have to press it again to check it.
Is it possible to improve this code to minimize the amount of code i will have to write in the future.
private void checkYes1_CheckedChanged(object sender, EventArgs e)
{
OnCheck(true);
}
private void checkNo1_cbx_CheckedChanged(object sender, EventArgs e)
{
OnCheck(false);
}
private void OnCheck(bool yes)
{
textBox14.Enabled = yes;
checkNo1_cbx.CheckedChanged -= checkNo1_cbx_CheckedChanged;
checkNo2_cbx.CheckedChanged -= checkYes1_CheckedChanged;
checkNo1_cbx.Checked = !yes;
checkNo2_cbx.Checked = yes;
checkNo1_cbx.CheckedChanged += checkNo1_cbx_CheckedChanged;
checkNo2_cbx.CheckedChanged += checkYes1_CheckedChanged;
}
However consider using RadioBox instead of CheckBox because you want to if one being checked uncheck the other..
Edit: In your previous design, you get it wrong changed I have to press it again to check it because of you have two event handlers assigned to each of the check boxes. now at your code when the first one checked, you are disable the text box and make the other unchecked, but when you call the other unchecked Checked = false you are calling the second check box event handler also so it will enable the text and make the first one disable... you should remove the event handler by -= when updating at your code if you don't want the handler handler to be triggered again.. And that what I am doing in the code sample provided.
Why are you using 2 checkoboxes ? One checkbox (check1) would be enough:
private void check1_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = check1.Checked;
}
EDIT:
Assuming that each question mean 1 textbox, then you need 1 checkbox per textbox... this could be further improved by using a more complex approach
unless there is a reason that you are making a round trip back and forth to the server to disabled a textbox on a checkbox selection change, why don't you just do that all on the client side via javascript?
I agree with Yahia. If you do need to explicitly provide the two options though, then you should consider using RadioButtons.
I want to change the text of the button with partial post backs. There a few things that i dont understand..
Button quote;
public void addButtonsPost()
{
quote=new Button();
quote.Click += quote_Click;
sm.RegisterAsyncPostBackControl(quote);
}
public void quote_Click(object sender, EventArgs e)
{
if (quote.Text == "quote")
{
quote.Text = "quote+";
}
else
{
quote.Text = "quote";
}
}
So basically, the text of the button should be executed each time and the text should change from quote to quote+ and vice versa. How do i achieve this...and do i need to use viewState to save the current button text between the partial postbacks or is it not necessary?
I think this post will help you with your problem.
How can I programmatically add triggers to an ASP.NET UpdatePanel?
Assuming your addButtonsPost call is working and registering the asycn post back, then you should just need to tell the update panel to refresh by calling UpdatePanel1.Update(). Please not UpdatePanel1 should be the Id of your update panel.