locked
Need help for imagebuttons onclick event RRS feed

  • Question

  • User872370986 posted

    hi everyone 

    i am trying to build a hostel room allotment system with ASP.net and C# in which i want that all the rooms should be displayed as a panel and the already full rooms should be in red color and available vacant rooms should be in green color. and if i click on the room image that should be entered in text box as room number allotted. kindly anyone help in this. as there are 6-7 hostels i need that every time i select a hostel it should check from database about the vacancy in rooms of tht hostel and display them in a panel as a table of images. please help me in this code.

    Thanks and Regards

    Indu Joshi

    Monday, April 16, 2018 6:48 AM

All replies

  • User283571144 posted

    Hi Indujoshi09,

    According to your description, we couldn't give you any code about your requirement.

    Since we all not have your database and asp.net codes.

    I suggest you could firstly post the codes you have used now and tell us the issue you have faced now.

    Besides, I suggest you could try to use jquery with ajax to achieve your requirement.

    You could use jquery bind the model popup with the image button.

    Then you could use ajax to get the data from database and fill the data into model popup.

    More details, you could refer to below exmaple:

     https://www.aspsnippets.com/Articles/Open-Show-jQuery-Dialog-Modal-Popup-after-AJAX-Call-Success.aspx 

    Best Regards,

    Brando

    Tuesday, April 17, 2018 7:40 AM
  • User872370986 posted

    if (DropDownList1.SelectedItem.Text == "Brahmaputra Hostel")
    {
    mc.connect();
    mc.ds.Clear();
    mc.comm.CommandText = "select roomno,roomcapacity,newcapacity from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.da = new SqlDataAdapter(mc.comm);
    mc.cb = new SqlCommandBuilder(mc.da);
    mc.da.Fill(mc.ds, "hostels");
    foreach (DataRow row in mc.ds.Tables["hostels"].Rows)
    {
    string available = mc.ds.Tables["hostels"].Rows[0].ItemArray.GetValue(0).ToString();
    int avail = Convert.ToInt32(available);
    if (avail ==0)
    {
    ImageButton img = new ImageButton();
    img.ID = "ImageButton1";
    img.ImageUrl = "~/Images/Booked_room_img.gif";
    img.Enabled = false;

    }
    else
    {
    var button = new ImageButton();
    button.ImageUrl = "Selected_room_img.gif";
    button.ID = "E76";
    button.Enabled = true;

    }
    }

    }

    can you tell me what is wrong with this code

    i wish to create image button on the basis of data field newcapacity. its an integer field and if value of this field is zero then Booked_room_img.gif should be there otherwise selected room image should be there.

    Wednesday, April 18, 2018 5:53 AM
  • User61956409 posted

    Hi Indu Joshi,

    if value of this field is zero then Booked_room_img.gif should be there otherwise selected room image should be there.

    In the code that you provided, I can find that you dynamically create ImageButton and specify ImageUrl etc properties for that ImageButton, but you do not add the dynamic ImageButton to a control container (such as Panel control) in your webform page, so the dynamic ImageButton will not be rendered in your web page. You can try to add your dynamic created controls to control container and check if it displays as expected.

    Container_Panel.Controls.Add(img);

    With Regards,

    Fei Han

    Sunday, April 22, 2018 6:24 AM
  • User872370986 posted

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Web.Security;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Drawing;
    using System.Drawing.Imaging;

    public partial class hostelmain : System.Web.UI.Page
    {
    public class myclass
    {
    public SqlConnection con = new SqlConnection();
    public SqlCommand comm = new SqlCommand();
    public SqlCommandBuilder cb = new SqlCommandBuilder();
    public SqlDataAdapter da = new SqlDataAdapter();
    public DataSet ds = new DataSet();
    public DataSet ds1 = new DataSet();
    public DataSet ds2 = new DataSet();
    public DataSet ds3 = new DataSet();
    public void connect()
    {
    con.ConnectionString = "Data Source=localhost;Initial Catalog=alumni;Persist Security Info=True;User ID=sa;Password=parulsaini";
    }
    public myclass()
    {
    //
    // TODO: Add constructor logic here .
    //
    }
    }
    myclass mc = new myclass();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    Button3.Visible = true;

    }

    protected void Button3_Click(object sender, EventArgs e)
    {
    Panel2.Visible = true;
    if (DropDownList1.SelectedItem.Text == "Sutlej Hostel")
    {
    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = "img1";
    img.AlternateText = "img1"; ;
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = "img2";
    img.AlternateText = "img2"; ;
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;
    Pnl11.Controls.Add(img);
    }
    }
    }
    }
    if (DropDownList1.SelectedItem.Text == "Alaknanda Hostel")
    {

    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = "img1";
    img.AlternateText = "img1";
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = "img2";
    img.AlternateText = "img2";
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;
    Pnl11.Controls.Add(img);
    }
    }
    }
    }

    if (DropDownList1.SelectedItem.Text == "Brahmaputra Hostel")
    {
    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = "img1";
    img.AlternateText = "img1";
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = "img2";
    img.AlternateText = "img2";
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;
    Pnl11.Controls.Add(img);
    }
    }
    }
    }
    if (DropDownList1.SelectedItem.Text == "International Hostel")
    {
    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = "img1";
    img.AlternateText = "img1";
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = "img2";
    img.AlternateText = "img2";
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;
    Pnl11.Controls.Add(img);
    }
    }
    }
    }
    if (DropDownList1.SelectedItem.Text == "Krishna Hostel")
    {
    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = "img1";
    img.AlternateText = "img1";
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = "img2";
    img.AlternateText = "img2";
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;
    Pnl11.Controls.Add(img);
    }
    }
    }
    }
    if (DropDownList1.SelectedItem.Text == "Narmada Hostel")
    {

    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = "img1";
    img.AlternateText = "img1";
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = "img2";
    img.AlternateText = "img2";
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;
    Pnl11.Controls.Add(img);
    }
    }
    }
    }
    if (DropDownList1.SelectedItem.Text == "Kaveri Hostel-Old Wing")
    {

    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = "img1";
    img.AlternateText = "img1";
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = "img2";
    img.AlternateText = "img2";
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;
    Pnl11.Controls.Add(img);
    }
    }
    }
    }

    if (DropDownList1.SelectedItem.Text == "Kaveri Hostel-Amrawati Wing")
    {

    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = "img1";
    img.AlternateText = "img1";
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    img.Click += new ImageClickEventHandler(img_Click);
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = "img2";
    img.AlternateText = "img2";
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;
    Pnl11.Controls.Add(img);

    }
    }
    }

    }
    if (DropDownList1.SelectedItem.Text == "Kaveri Hostel-Hemawati")
    {
    Pnl11.Visible = true;
    mc.connect();
    mc.ds.Clear();
    string selectSQL = "select * from hostels where hostelname=" + "'" + DropDownList1.SelectedItem.Text + "'";
    mc.comm.Connection = mc.con;
    mc.con.Open();
    SqlCommand cmd = new SqlCommand(selectSQL, mc.con);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr != null)
    {
    while (dr.Read())
    {
    int capacity = Convert.ToInt32(dr["newcapacity"]);
    string room_no = Convert.ToString(dr["roomno"]);
    if (capacity == 0)
    {
    ImageButton img = new ImageButton();
    img.ID = room_no;
    img.AlternateText = room_no ;
    img.ImageUrl = "~/Images/booked_img.gif";
    img.Enabled = false;
    Pnl11.Controls.Add(img);

    }
    else
    {
    ImageButton img = new ImageButton();
    img.ID = room_no;
    img.AlternateText = room_no ;
    img.ImageUrl = "~/Images/selected_img.gif";
    img.Enabled = true;

    Pnl11.Controls.Add(img);
    }
    }
    }

    }

    }
    void img_Click(object sender, ImageClickEventArgs e)
    {
    TextBox3.Text = ((ImageButton)sender).ID.ToString();
    }

    }

    The img_Click() function is not working as i want that image id should be entered in text box  whenever i click on the image button But instead after click  the panel disapears and also alternate text is not displayed on the image button. if anybody can help me sort out this problem.

    Monday, April 23, 2018 5:32 AM