如何使用gridview中的图像按钮重定向到另一个页面?

时间:2013-08-23 05:23:18

标签: asp.net gridview datagridview

如何使用放置在gridview中的图像按钮重定向到另一个页面,方法是检查其他列中的项目模板(标签)值是否等于给定文本。

this is my code:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter("select ID,SurveyName from SurveyMaster1  union select -1,'Select'", con);
            da.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataTextField = "SurveyName";
            DropDownList1.DataBind();
            DropDownList1.SelectedValue = "-1";
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("SELECT Question,QuestionType FROM Questions  WHERE SurveyID = '"+ DropDownList1.SelectedValue.ToString() +"'" , con);
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


protected void imgbtnEdit_Click(object sender, ImageClickEventArgs e)
    {
 GridViewRow grdSelRow = GridView1.SelectedRow;

        TextBox textInt = (TextBox)GridView1.FindControl("text1");

        if (textInt.Text == "Text")
        {
            Response.Redirect("Text.aspx");
        }            
    }

任何人都请帮助我..可能是我的问题不太清楚。

3 个答案:

答案 0 :(得分:1)

protected void imgbtnEdit_Click(object sender, ImageClickEventArgs e)
{
    ImageButton imgbtnEdit = (ImageButton)sender;
    GridViewRow gr = (GridViewRow)imgbtnEdit.NamingContainer;
    TextBox textInt = (TextBox)gr.FindControl("text1");
    if (textInt.Text == "Text")
    {
        Response.Redirect("Text.aspx");
    }
}

试试这段代码。

答案 1 :(得分:0)

尝试以下操作:更改(TextBox)GridViewRow.FindControl("text1");

 protected void imgbtnEdit_Click(object sender, ImageClickEventArgs e)
        {
     GridViewRow grdSelRow = GridView1.SelectedRow;

            TextBox textInt = (TextBox)GridViewRow.FindControl("text1");

            if (textInt.Text == "Text")
            {
                Response.Redirect("Text.aspx");
            }            
        }

答案 2 :(得分:0)

在gridview的RowCommand事件中编写代码

提供图像按钮CommandName property =“Edit”

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Edit"))
            {


        TextBox textInt = (TextBox)GridView1.Rows[e.RowIndex].FindControl("text1");

        if (textInt.Text == "Text")
        {
            Response.Redirect("Text.aspx");
        }   
        }
    }