嘿,朋友,我想,以避免對圖像按鈕的點擊回傳,這裏是我的代碼: -避免自動回
它有一個圖像按鈕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();
}
感謝Bryan的工作,但現在圖像按鈕必須點擊兩次..才能看到更改 – 2012-02-21 07:51:38
您是否查看由服務器呈現的HTML?什麼導致回發?您可能想嘗試僅使用LinkButton。 – 2012-02-21 16:53:23