سؤال

- What I am making is a webform1 which displays a list of car names. 
- Each car has a corresponding button which directs to webform2 where the image of the 
  corresponding car is displayed. 
- I am storing the Image of each car in a folder in the project. 

What algorithm or code should i put on the button for it to load the a different image based on the corresponding car name it is clicked on.

هل كانت مفيدة؟

المحلول

You could do something like this:

Child form

public string CarSource{ get; set; }

public TestForm()
{
    InitializeComponent();
}

public void UpdateValues()
{
    Image.Source = CarSource;
}

Initiate it

var child = new TestForm {CarSource = someTextBox.Text};

child.UpdateValues();

child.ShowDialog();

نصائح أخرى

You can use QueryString for the same, firstly pass the names of cars from webform1 to webform2:

for e.g. on the click of button on WebForm1:

    protected void btn1_Click(object sender, EventArgs e)
    {
        Response.Redirect("WebForm1.aspx?FirstName=" + txt1.Text + "&LastName=" + txt2.Text);
    }

Now as you are saying that you are having an Image corresponding to each Image so you can take an ASP:HyperLink whose Image path can be set according to the name of car like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txt3.Text = Request.QueryString["FirstName"];
            txt4.Text = Request.QueryString["LastName"];
            if (Request.QueryString["FirstName"] == "Mohit")
            {
                hyper.ImageUrl="~/image1.jpg";
            }
            else
            {
                hyper.ImageUrl = "~/image2.jpg";
            }
        }
    }

Here "hyper" is the ID of Hyper Link which I had taken on design page.

I hope this will solve your problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top