Avatar of leap29
leap29
Flag for Afghanistan

asked on 

Image button databind datalist

I have a datalist which has several objects which have databound to them.  The problem I'm having is binding data to my imagebutton.  

The datalist shows details of jobs in our database and has a image button to shortlist each job.  The problem i'm having is that the image button is taking a the jobid of the first job displayed in the datalist.  Please find my code below.
protected void jobbasket_Click(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection objConn = new System.Data.SqlClient.SqlConnection();
        objConn.ConnectionString = "";
        objConn.Open();
 
        string cusername = string.Empty;
        string Jobidbasket = DataList2.DataKeys[0].ToString();
        if (HttpContext.Current.Request.IsAuthenticated)
        {
            cusername = HttpContext.Current.User.Identity.Name;
        }
        else
        {
            cusername = null;
            Response.Redirect("pleaseregister.aspx");
        }
        System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand("SQLSP", objConn);
        objCmd.CommandType = System.Data.CommandType.StoredProcedure;
 
        System.Data.SqlClient.SqlParameter jobid = objCmd.Parameters.Add("@jobid", System.Data.SqlDbType.Int);
        System.Data.SqlClient.SqlParameter cuser = objCmd.Parameters.Add("@currentuser", System.Data.SqlDbType.NVarChar);
 
        jobid.Direction = System.Data.ParameterDirection.Input;
        jobid.Value = Jobidbasket;
        cuser.Direction = System.Data.ParameterDirection.Input;
        cuser.Value = cusername;
        try
        {
            objCmd.ExecuteNonQuery();
 
        }
        catch (SqlException ex)
        {
            string popupScript = "<script language='javascript'>" +
            "window.alert('You have already added this job');" +
            "</script>";
            Page.RegisterStartupScript("PopUpScript", popupScript);
        }
 
        objConn.Close();
    }
 
 
 
-------image button 
 
<asp:ImageButton ID="imagebutton1" runat="server" 
                              CommandArgument='<%# Bind("jobid") %>'   CommandName="DetailsViewRedirect"
                              ImageUrl="Images/icn_jb_bsket.gif" 
                                                            OnClick="jobbasket_Click" style="height: 16px" />

Open in new window

.NET ProgrammingVisual C++.NET

Avatar of undefined
Last Comment
stphivos

8/22/2022 - Mon