change image on postback - c#

So, here's the scenario:
I have a page in my asp.net c# based website, which displays data dynamically after taking the request from query strings.
For example:
Brand=HTC&model=Wildfire
Now, in this page I want to display all the deals available in each network separately.
So, on the first pageload, I retrieve the data from the tables of each network that has the requested handset in a deal, store it as a datatable in the cache.
I have a datalist for displaying the imagebuttons of the networks that have the deals of that handset. The user will click on the imagebutton of a network to view the deals of that network.
To display the deals, I am using a gridview which retrieves its datasource from the cache. Also, I have provided filtering of the rows through dropdowns in each columns' header of the gridview.
All this is inside an updatepanel.
Everything's working fine.
The Problem:
I want that whenever the user clicks a network's image button, an image should appear above the gridview, containing the logo of that network.
Things that I have tried:
I am storing the networkname in the cache(which also required for filtering the gridview), whenever an imagebutton is clicked. Then, on pageload, I'm doing this:
if(ScriptManager1.IsInAsyncPostBack)
{
Image img = new Image();
img.ImageUrl = "images/" + Cache["network"] + "_logo.jpg";
nw_image.Controls.Clear();
nw_image.Controls.Add(img);
}
where, nw_image is a tablecell which i'm using to display the selected network logo.
and also I've saved the network logos in the images folder as: 'network'_logo.jpg
eg: 3_logo.jpg, orange_logo.jpg
The result of this is that when I click a new image button, the image that is displayed is that of the previous network, i.e. the cache is always one postback late.
Please Help!
Thanks!

Rather than using dynamic controls (i.e. creating the Image control manually on a postback), it would be much easier to just define an Image control in your page markup and default its Visible property to false. Then on your postback, set the ImageUrl property to the correct URL, and the Visible property to true.
Also, ensure that your nw_image table cell is inside your UpdatePanel, and you either have ChildrenAsTriggers set to true on the update panel (this is the default), or have specified an AsyncPostbackTrigger for the Click event of the ImageButton.

At last I've got it to work.
I've now put a static image without any properties except id = "net_img" , runat = "server" and EnableViewState = "true".
And instead of taking the network name from the cache, I'm changing the image in the click event of the ImageButton according to the Imagebutton that is clicked.
And it works now! :)

Related

C#: How can I programmatically loop through all controls on a Windows Form

I'm in the process of creating a front-end Windows Forms application, which will have multiple uses. One of those uses will be to allow the end-user to manage the contents of a database table.
At the moment, my form contains a DataGridView object which uses a DataTable as its source. That datatable is populated from a stored procedure which selects every row from the table based on criteria specified by the end user. I want to put code into the DataGridView which will look at the row that's been selected by the user, and will populate a series of text boxes with the details from that row.
I want to avoid hard-coding a text box for each column in the DataGridView, since it's highly possible that in the future I might add columns to the underlying stored procedure. Therefore, my plan is that the first time the user selects a row, the application would dynamically create a text box for each column, size that text box appropriately (both height and width) and add the appropriate labels.
So far, the code I've written creates a brand-new DataTable, and adds columns to store the following information about each column in the DataGridView:
The actual name of the DataGridView column;
The name that I wish to give to the associated label;
The text that I wish to assign to the associated label;
The top and left positions of the associated label;
The name that I wish to give to the associated text box;
The value that I wish to display in the associated text box;
So far when the user initially clicks on a row, everything is working well: the text boxes get created in a vertical column, with the associated label to the left of each text box and the appropriate value displayed in each one. Each text box has its width dynamically set based on the contents of that text box so that everything can easily be seen.
The difficulty comes when the user changes from one row of the DataGridView to another. The code that runs to create each control looks like this:
if (pnlManageJobManagerOutcomes.Controls.Contains(txtCurrent))
{
pnlManageJobManagerOutcomes.Controls.Remove(txtCurrent);
}
if (!pnlManageJobManagerOutcomes.Controls.Contains(txtCurrent))
{
pnlManageJobManagerOutcomes.Controls.Add(txtCurrent);
}
When the code first runs, all seems to be fine. However, if it gets fired a second time (at which point it should remove and re-create the control) it doesn't seem to detect the prior existence of the control and simply creates a second copy of it in the form.
For information, the form I'm building has multiple uses which are dictated by a series of buttons. Therefore, I'm creating the controls for this particular use inside a Panel (pnlManageJobManagerOutcomes). I'm completely stumped as to why the code doesn't spot the existence of those controls during subsequent executions.
TIA
I've figured out a way to do it, which may seem to be clunky but which seems to work.
Since it doesn't appear that I can check to see whether the Panel I'm working with Contains the field name, I've written a method which will loop through the controls in that Panel and return a count of the number of instances of that control in the Panel's Controls array. If the method returns a 0, this means the Control doesn't exist in that Panel and I can go ahead and create it.

Retrieve and save gridview in session

I have a page for searching when I write my name for example in textbox it will search and display the results in a GridView. I'm using an EntityDataSource with a QueryExtender so I didn't write any code in C#.
The problem is: in the GridView I have a hyperlink when I click it, it will go to another page, ok fine then when I return to the previous page the GridView does not show the results of the search when I was left because of the postback (update panel) it show the whole data from EntityDataSource.
I think I have to use session but I don't know how I can do it, I mean how I can save the GridView in session and how I can retrieve it in page_load.

How to create a gridview in asp.net having features similar to the datagridview in Windows forms

I understand that the Gridview of asp.net and windows forms are different, but my requirement is that I have a registration form where in a user registers his vehicles.
A single user can have many vehicles, so I want to provide a Gridview with the column names specified and allow users to enter the values in the rows. Depending on the number of vehicles, the user keeps adding them. This task is actually easy in the Windows forms since the grid is in such a way that an empty row is provided by default and we can type in the cells. Finally we can read the DataGridview by each row and save the results to database.
I am looking for this feature in the Asp.net either with the Gridview or any other data control. This looks pretty complex with the asp.net gridview since ill need to work with the edit button, update button for doing this every time and also a add new row button every time.
Is there any other way I can do this task without complexity. I am fairly new to asp.net.
If you want to add a new row then what you can do it add a footer row to the gridview. and everytime the user enter information in the footer row you can bind it to gridview and when the page is reloaded you will always have the footer row. This way user can register as many cars they want.

how to load page with editable grid view with it's data?

really i think it is very complicated i hope it is not
my goal like this but this windows app
Image
i have abutton to browse an images folder i want user when choose image folder
my page load and appear agridview in editable mode and show images data i loaded from folder
user can change every row(every image) data
and click save button to save all images in database
i don not know how can i do this
can anybody help me plz
If I am understanding you correctly, I don't believe you need to have the GV in edit mode, you just need to include and upload control in each row. That way a user can select a file for each row and on button click you can loop through the GV and upload the file (if selected). As far as the GV setup, just bind as you normally would with an if (!Page.IsPostBack).
foreach (GridViewRow g in myGV.Rows)
{
FileUpload f = g.FindControl("myFileUpload") as FileUpload
if (f.HasFile)
{
// upload file
}
}
i used a fantastic jquery API called Uploadify

Displaying image on a different page

Hallo there
I have and imageField as part of a gridview and would like to know how I can view this image in a different page when I select the row with a button(field).
When I add the information to a database the imageUrl is added to the database and this I tie up with the name of an uploaded image and subsequently the image is displayed in the gridview.
Is it possible for me to display this image on a different web page as well
thanks
Instead of storing image in session, why not populate Session["ImageID"] value on Button click?
Set gridview's OnRowCommand property to a method that will set Session["ImageID"] to the current row's DataKey, then do a redirect to that other page that will get value from Session["ImageID"] and get image URL for that ID from database. Don't forget to also perform Session.Remove("ImageID"). That way you'll keep your session object as small as possible.
Yes ,you can , i think if you store the selected image information in a Session.May be, there are other ways , but the first thing comes in my mind in this case using the session.

Categories

Resources