Application design, separated logic, delegate or not - c#

Delegates, would those apply here? If so, how? Code examples, as I am new to this area.
Logic separation is what I am intending to implement and I think my task below would be a perfect candidate. I have read that objects should be fairly independent and not hard wired to one another.
ASPX (Page)
<uc1:Attachment ID="Attachment1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
ASPX.cs (Codebehind)
protected void Button1_Click(object sender, EventArgs e)
{
// Check the files and upload
}
ASCX (UserControl)
<MyControl:Upload ID="Upload1" runat="server"
AllowedFileExtensions=".zip,.jpg,.jpeg,.doc,.gif,.png,.txt"
MaxFileInputsCount="3"
OverwriteExistingFiles="false"
onfileexists="Upload1_FileExists"
onvalidatingfile="Upload1_ValidatingFile" />
ASCX.cs (Codebehind)
public Upload AttachmentControl
{
get { return this.Upload1; }
}
The task above is a file upload module. The control that does the upload is in a UserControl which has been dragged on to an ASPX page.
The Submit button on the ASPX page should start the file upload process.
This should be a fairly common scenario where the Page saves other information to a database and also upload files.
My instinct from past experience was to expose the Upload control in the UserControl via a public Property.
This would tie the Attachment UserControl to the the page.
How can I untie this, perhaps the use of delegates?

What information does the view need about its Attachment control? What does the Attachment control need to know about the view which contains it?
I suppose a delegate(Stream uploadContent) could let you decouple this farther, but if you're using the System.Web.Page model... the motivation to decouple things that far seems lacking.
Having the view accept a callback inverts the appropriate path of dependency in your application, doesn't it?

Related

Using FileUpload on a modal popup and handling postbacks

I'm stuck in a catch-22 situation...
I have an ASP.NET 4.0 Webforms application, that uses model popups (using the Ajax Control Toolkit's ModalPopupExtender component) to get inputs from user.
One screen is used to add new members, or edit existing member's records. These records include a picture of the member, which I'm uploading using a standard ASP.NET FileUpload control.
That create/edit screen is wrapped in an UpdatePanel so that when I pop up the overlay, I can fill in the textboxes with existing values (when editing an existing member).
Since the popup uses an UpdatePanel, I had to define an explicit PostBackTrigger so that my Save button will actually cause a complete postback and so that I'm able to read up the bytes of the picture uploaded in the FileUpload control.
<asp:UpdatePanel ID="pnlCreateOrEdit" runat="server">
<ContentTemplate>
.... asp.net controls here, including:
<asp:FileUpload ID="uploadPic" runat="server" />
....
<asp:Button ID="btnSave" runat="server" OnClick="BtnSaveClicked" Text="Save" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSave"/>
</Triggers>
</asp:UpdatePanel>
If I don't define that PostBackTrigger, then my uploaded file can't be read out.
BUT: once I define that postback trigger, now I cannot do my validation anymore, since if I do my validation in the btnSaveClicked method and it fails (something is missing for instance), I can detect that - but I can't respond to it anymore, since the popup will be closed no matter what I've tried so far....
protected void BtnSaveClick(object sender, EventArgs e) {
// do my validation - if something is amiss, don't go any further
if (!IsValid()) {
return;
}
// read back the data
// hide the modal popup form
// call an event handler to process the inputs
}
So now I'm stuck:
if I omit the post back trigger, my validation works nicely - if something is wrong, it's flagged and shown on the popup form, and the popup isn't closed
==> BUT in that case I cannot read out the bytes of the uploaded picture..... (uploadPic.HasFile is always false)
if I put in the post back trigger, my file upload works nicely, I can read out my bytes no problem
==> BUT now, my validation - while detecting something's amiss - cannot show the error since the popup form is being closed by the full postback....
Is there any way to get BOTH - the bytes in the FileUpload control, AND the popup staying open if validation fails?
This popup will give you complete control with out any post back issues.
you can see this in action here: http://atldunia.com/youtube/Zpopup.aspx
you can also download the code.
I ended up using the AsyncFileUpload component from the Ajax Control Toolkit - this works nicely on an UpdatePanel and satisfies all my needs.

Dynamic data load on ASPX page

I have an internal web service (ASP.NET) written on C# in a company I work for. There are only 2 pages in it, one of this pages contains DropDownList.
Every time when user selecting an item from that DropDownList I need to somehow pass selected item value to a static method and show result string of that method anywhere on page.
I've never worked with ASP.NET or any web programming before and a bit confused about how to do it, not sure where to start looking even.
In your aspx file you should have this:
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
onselectedindexchanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
Notice the AutoPostBack="True" which goes back to the server and fires the selectedindexchanged event immediately after the user changes the selection in the listbox
In your code-behind (.cs file)
You should have this:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Call static method and pass the ListBox1.SelectedIndex
// MyStaticMethod(ListBox1.SelectedIndex);
}
you can either se5t the autoPostBack="true" and handle the change event on the server side or using jQuery subscribe for the change event and get the value on the client side
You should probably check out some of the great resources that microsoft provides for new .NET developers. They will be really helpful in getting you started. Her is a link of some really good videos to help you out: http://www.asp.net/web-forms/videos
Not sure what language you are coming from, if any... But for the most part webforms is going to work a lot like other web based methodologies.
Your ASP.NET Controls (in your case the DropDownList) have both client and server side events.
You will probably want to map the server-side OnSelectedIndexChanged event on your DropDownList.
In order to cause a postback on that control you will want to set the AutoPostBack property to true on your DropDownList.
try this one
In html ,
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
In aspx.cs page,,
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string selctedValue = DropDownList1.SelectedValue;
/// Call yours static methid here
YourMethod(selctedValue);
}

Working with Request.Files and Changing from POST to GET without reloading page?

I am trying to figure out if I have something wrong with my architecture, or if I am just in need of a quick fix.
I have the following on my page:
<div ID="UploadDashboardDecorationZone">
<fieldset id="UploadDashboard">
<legend>Upload Dashboard</legend>
<telerik:RadUpload ID="UploadDashboardSelector" Runat="server" Width="235px" AllowedFileExtensions=".xml" MaxFileInputsCount="1" ControlObjectsVisibility="None" />
</fieldset>
<div class="BottomButton">
<telerik:RadButton ID="SubmitUploadDashboardButton" Runat="Server" Text="Upload" OnClientClicked="CloseUploadDashboard" />
</div>
</div>
The user selects a file they wish to upload, then they press the SubmitUploadDashboardButton.
I then have the following server-side code:
protected void Page_Init(object sender, EventArgs e)
{
if (Request.Files.Count > 0) HandleUploadedFile();
}
This all works great. The uploaded file is responded to and I see the changes on my page. The only problem is that I have left my page's request HTTP Method as POST and not GET. This means that bad things will happen if the user refreshes the page.
Previously, I had used the following code-snippet to resolve this issue:
Page.Response.Redirect(Page.Request.Url.ToString(), true);
Unfortunately, this does not work for me anymore. I don't want the flashing that happens with reloading the page (in addition to some code issues that arise with reloading).
What are my other options here? If I wrap UploadDashboardDecorationZone with an UpdatePanel, then Request.Files comes out as 0.
Is there a quick code-fix for this that is common? Or am I missing something deeper in my understanding of how the file upload process works?
Thanks.
I don't know of a way to get around the "flash" when the page changes unless you do your calls using Ajax which doesn't require the page to reload at all.

Passing parameters to a User Control

We are trying here to localize our user control basically we want to be able to do something like this :
<in:Banner runat="server" ID="banners" Lang="fr" />
The way we do this is by page level and send it to master that then send it to the control:
protected void Page_Load(object sender, EventArgs e)
{
Master.Lang = "FR";
}
Then in the MasterPage.master we do something like this :
<in:Banner runat="server" ID="banners" Lang="<%= Lang %>" />
The masterpage has a public proprety named Lang.
In the control we have set a field that contains the default language and a proprety (Lang) that set the language. It seems that whatever we do, the current language is not sent from the page to the usercontrol... any help?
Not exactly like that, but you can consider the content page like a control in the master page, so its likely that the page load of the page is executing before the page load of that user control.
Regardless of the above, why not set the ui culture to the asp.net thread (probably from the global.asax), and using that from the control.
Another alternative is to have a separate class where you hold the current language, and expose a change event ... that way you can make sure to use the right language even if the load is done differently --- or there is a change language event later.
You can access it in code-behind of the MasterPage like this
public void SetLanguage(string language)
{
banners.Lang = language; //banners is an ID reference to your user control.
}
Or in your markup I think you can do it like this
<in:Banner runat="server" ID="banners" Lang='<%# Bind("Lang") %>' />
I should mention that Bind works in .Net 2.0 and up.

Attach RequiredValidator on custom server control rendering a TextBox

I don't know whether this is really possible, but I'm trying my best.
If I have a (complex) custom server control which (beside other controls) renders a TextBox on the UI. When placing the server control on a page, would it be possible to attach a RequiredField validator to that server control, such that the validator validates the Text property of that control which points to the Text property of the rendered TextBox?
Of course I could incorporate the RequiredField validator directly into the server control, but this is for other reasons not possible (we are rendering RequiredField validators automatically on the UI).
Thanks for your help.
I think one solution is to put your TextBox control inside a Panel then you add the RequiredValidator control dynamically on the Page_Load event handler.
<asp:Panel ID="Panel1" runat="server">
<MyCustomTextBox ID="TextBox1" runat="server"></MyCustomTextBox>
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Button" />
then
protected void Page_Load(object sender, EventArgs e)
{
var validator = new RequiredFieldValidator();
validator.ControlToValidate = "TextBox1";
validator.ErrorMessage = "This field is required!";
Panel1.Controls.Add(validator);
}
I put the CustomTextBox inside the panel to assure that the validation controle place is correct when added
I got it, the 2nd time that I'm answering to my own post :) Next time I'll do a deeper research before.
For those of you that may encounter the same problem. You have to specify the ValidationProperty attribute on your server control's class. For instance if your server control exposes a property "Text" which is displayed to the user and which should also be validated, you add the following:
[ValidationProperty("Text")]
Then it should work.

Categories