2012-02-21 99 views
0

嘿,朋友,我想,以避免对图像按钮的点击回传,这里是我的代码: -避免自动回

它有一个图像按钮gridview的编辑选择的行

<form id="form1" runat="server"> 
    <asp:Label ID="lblsearch" Text="Search by" runat="server"></asp:Label> 
    <asp:DropDownList ID="ddlsearch" runat="server" OnSelectedIndexChanged="SearchProject" AutoPostBack="true"> 
    <asp:ListItem Text="Select" Value="select" Selected="True"></asp:ListItem> 
    </asp:DropDownList>       
    <asp:Button ID="btnclear" runat="server" Text="Clear" OnClick="btnclear_Click" /> 
    <asp:Label ID="lblsearchmsg" runat="server" ForeColor="#FF3300"></asp:Label> 
    <br /> 
    <asp:GridView ID="gviewprojectallocation" runat="server" CellPadding="4" 
     ForeColor="Black" GridLines="Vertical" EnableViewState="true" 
     AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" 
     BorderStyle="None" BorderWidth="1px" 
     onrowcommand="gviewprojectallocation_RowCommand" 
     onrowcancelingedit="gviewprojectallocation_RowCancelingEdit" 
     onrowediting="gviewprojectallocation_RowEditing" 
     onrowupdating="gviewprojectallocation_RowUpdating"> 
     <AlternatingRowStyle BackColor="White" /> 
     <FooterStyle BackColor="#CCCC99" /> 
     <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> 
     <RowStyle BackColor="#F7F7DE" /> 
     <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 
     <SortedAscendingCellStyle BackColor="#FBFBF2" /> 
     <SortedAscendingHeaderStyle BackColor="#848384" /> 
     <SortedDescendingCellStyle BackColor="#EAEAD3" /> 
     <SortedDescendingHeaderStyle BackColor="#575357" /> 
     <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
      <asp:ImageButton CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>' runat="server" id="ImageButton1" ImageUrl="~/images/edit.png" CommandName="Edit"/> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="associate_id" ReadOnly="true" HeaderText="Associate ID"/> 
     <asp:BoundField DataField="Associate_Name" ReadOnly="false" HeaderText="Associate Name" /> 
     <asp:BoundField DataField="involve_percent" ReadOnly="false" HeaderText="Involve %" /> 
     </Columns> 
    </asp:GridView> 
</form> 
<br /> 
</center> 
背后

代码: -

//在页面加载加载在下拉列表信息

protected void Page_Load(object sender, EventArgs e) 
{ 
    SqlConnection myconnection = new SqlConnection(constring); 
    SqlCommand mycommand = new SqlCommand(); 
    mycommand.Connection = myconnection; 
    int i = 1; 

    SqlDataReader mydatareader = null; 
    myconnection.Open(); 
    mycommand.CommandText = "select Project_Code,Project_Name from Project_Status_Report;"; 
    mycommand.CommandType = CommandType.Text; 

    mydatareader = mycommand.ExecuteReader(); 
    if (!IsPostBack) 
    { 
    while (mydatareader.Read()) 
    { 
     ddlsearch.Items.Add((string)mydatareader["Project_Name"]); 
     ddlsearch.Items[i].Value = Convert.ToString(mydatareader["Project_Code"]); 
     i++; 
    } 
    } 
    myconnection.Close(); 
} 

//对于值的下拉列表中选择的基础上进行搜索: -

protected void SearchProject(object sender, EventArgs e) 
{ 
    try 
    { 
    SqlConnection myconnection = new SqlConnection(constring); 
    SqlCommand mycommand = new SqlCommand(); 
    DataSet mydataset = new DataSet(); 
    SqlDataAdapter mydataadapter = new SqlDataAdapter(); 

    if (ddlsearch.SelectedValue == "select") 
    { 
     Response.Redirect("ProjectAllocation.aspx"); 
    } 
    else 
    { 
     mycommand.CommandText = "select P.associate_id,T.Associate_Name,P.involve_percent from Associates_Info as T inner join Associate_Project as P on T.Associate_ID=P.associate_id where P.project_code = @procode;"; 
     mycommand.Parameters.Add("@procode", SqlDbType.Int); 
     mycommand.Parameters["@procode"].Value = ddlsearch.SelectedValue; 

     mycommand.CommandType = CommandType.Text; 
     myconnection.Open(); 
     mycommand.Connection = myconnection; 

     mydataadapter.SelectCommand = mycommand; 

     mydataadapter.Fill(mydataset); 

     if (mydataset == null || mydataset.Tables.Count == 0 || mydataset.Tables[0].Rows.Count == 0) 
     { 
     lblsearchmsg.Text = "Record not found"; 
     } 

     gviewprojectallocation.DataSource = mydataset; 
     gviewprojectallocation.DataBind(); 

     myconnection.Close(); 
    } 
    } 
    catch (Exception exp) 
    { 
    lblsearchmsg.Text = "Enter valid information"; 
    } 
} 

//行的GridView的命令: -

protected void gviewprojectallocation_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Edit") 
    { 
    int index = Convert.ToInt32(e.CommandArgument); 
    GridViewRow row = gviewprojectallocation.Rows[index]; 
    } 
} 

//网格视图行编辑事件

protected void gviewprojectallocation_RowEditing(object sender, GridViewEditEventArgs e) 
{       
    gviewprojectallocation.EditIndex = e.NewEditIndex; 
    gviewprojectallocation.DataBind(); 
} 

回答

0

如果我正确阅读您的问题,您可能需要尝试将OnClientClick='return confirm_ambiguous_functionality();'添加到您的ImageButton以及一些javascript的定义中。或者你可以简单地让它不返回(例如,javascript:void(0);),这取决于预期的功能。

<script type="text/javascript"> 
    function confirm_ambiguous_functionality() { 
     return confirm("Are you sure you want to do whatever action you just clicked on?"); 
    } 
</script> 
+0

感谢Bryan的工作,但现在图像按钮必须点击两次..才能看到更改 – 2012-02-21 07:51:38

+0

您是否查看由服务器呈现的HTML?什么导致回发?您可能想尝试仅使用LinkBut​​ton。 – 2012-02-21 16:53:23

0

我认为这与你的情况去最好的情况是把你的GridView在一个更新面板,使整个工序的部分后回来,因为你需要的图像按钮,进入到服务器,因为你正在说图像按钮用于编辑选定的行。