简体   繁体   中英

Trigger Click event on the first repeater item

I'm making a gallery site. And it plots the available photography sessions that are on the database.

But when the user first opens the page, no photography sessions are selected. So the page would be empty. I want to automatically select the first session for the user. I did it by selecting the first record in the database. But i really wish if i could just write a code inside the Page_load that auto clicks on the first item inside the repeater.

The repeater items are dynamically generated of course. So i don't know if i could achieve this using JavaScript or not.

Here's my code :

protected void Page_Load(object sender, EventArgs e)
{

    DataSet dst = new DataSet();
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
    {
        SqlDataAdapter adbtr = new SqlDataAdapter();                
        adbtr.SelectCommand = new SqlCommand("SELECT * FROM dbo.Select_gallery_names_FN()", con);
        try
        {
            int result = adbtr.Fill(dst);
            if (result == 0)
            {
                return;
            }
            cat_repeater.DataSource = dst;
            cat_repeater.DataBind();
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }

    }

    using (SqlConnection img_con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
    {
        SqlDataAdapter img_adbtr = new SqlDataAdapter();
        img_adbtr.SelectCommand = new SqlCommand("select * from dbo.Select_gallery_cat_FN(@img_cat)", img_con);
        img_adbtr.SelectCommand.Parameters.Add("@img_cat",SqlDbType.NVarChar,8000).Value = dst.Tables[0].Rows[0][0].ToString();
        DataSet img_dst = new DataSet();                
        try
        {
            img_adbtr.Fill(img_dst);
            slider_repeater.DataSource = img_dst;
            slider_repeater.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }                
    }            
}

protected void cat_repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select * from dbo.Select_gallery_cat_FN(@img_cat)";
        cmd.Parameters.Add("@img_cat", SqlDbType.NVarChar,8000).Value = ((LinkButton)e.CommandSource).Text;
        con.Open();
        slider_repeater.DataSource = cmd.ExecuteReader();
        slider_repeater.DataBind();
    }
}

You could do something like this.

<script>
    $(document).ready(function() {
       $('#cat_repeaterId').find('tr:first').click();
    });
</script>

Might have to mess around with the javascript depending on if you want to click a button inside the first row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM