Panel1
Panel2
aspx
<!-- 輸入舊密碼 -->
<asp:Panel ID="Panel1" runat="server" defaultbutton="Button1">
<table style="width:100%;text-align:center;" border='1'>
<tr>
<td><asp:Label ID="Label1" runat="server" Text="員工編號:"></asp:Label></td>
<td><asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox></td>
<td rowspan="3" class="auto-style2"><asp:Button ID="Button1" runat="server" Text="立即變更密碼" OnClick="Button1_Click" /></td>
</tr>
<tr>
<td><asp:Label ID="Label2" runat="server" Text="員工姓名:"></asp:Label></td>
<td WORD-WRAP: break-word><asp:TextBox ID="TextBox2" runat="server" ReadOnly="True"></asp:TextBox></td>
</tr>
<tr>
<td class="auto-style1"><asp:Label ID="Label3" runat="server" Text="輸入舊密碼:"></asp:Label></td>
<td WORD-WRAP: break-word class="auto-style1"><asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
</table>
</asp:Panel>
<!-- End輸入舊密碼 -->
<!-- 輸入新密碼 -->
<asp:Panel ID="Panel2" runat="server">
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" DataSourceID="SqlDataSourceEmp" AutoGenerateRows="False" DataKeyNames="Emp_Num" OnItemUpdating="DetailsView1_ItemUpdating" OnItemUpdated="DetailsView1_ItemUpdated" Width="100%">
<Fields>
<asp:BoundField DataField="Emp_Num" HeaderText="員工編號" SortExpression="Emp_Num" ReadOnly="True" >
<HeaderStyle Width="100%" /><ItemStyle Width="100%" />
</asp:BoundField>
<asp:BoundField DataField="Emp_CnName" HeaderText="員工姓名" SortExpression="Emp_CnName" ReadOnly="True" />
<asp:BoundField DataField="Emp_Dep" HeaderText="部門/單位" SortExpression="Emp_Dep" ReadOnly="True" />
<asp:BoundField DataField="Emp_Title" HeaderText="職稱" SortExpression="Emp_Title" ReadOnly="True" />
<asp:BoundField DataField="Emp_Title_Num" HeaderText="系統預設職等" SortExpression="Emp_Title_Num" ReadOnly="True" />
<asp:BoundField DataField="Emp_Supervisor" HeaderText="直屬主管員編" SortExpression="Emp_Supervisor" ReadOnly="True" />
<asp:BoundField DataField="Emp_Mail" HeaderText="帳號/Mail" SortExpression="Emp_Mail" ReadOnly="True" />
<asp:TemplateField HeaderText="密碼" SortExpression="Emp_PW">
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text="輸入新密碼:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Password" Text='<%# Bind("Emp_PW") %>'></asp:TextBox>
<br />
<asp:Label ID="Label5" runat="server" Text="再輸入一次新密碼:"></asp:Label>
<br />
<asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Emp_PW") %>' ></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Emp_PW") %>' Visible="False"></asp:Label>
<asp:Label ID="Label6" runat="server" Text="********"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="更新" />
<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" OnClick="Button2_Click" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="編輯" />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</asp:Panel>
<!-- End輸入新密碼 -->
cs
protected void Page_Load(object sender, EventArgs e)
{
DefenseLogin(); //檢查是否登入
string struNum = (string)(Session["u_Num"]); //登入工號
string uName = (string)(Session["u_Name"]); //登入名稱
var value = ((Label)User_Name.FindControl("LoginInfo"));
value.Text = uName; //LoginInfo
TextBox1.Text = struNum;
TextBox2.Text = uName;
DetailsView1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
string TB1 = TextBox1.Text; //已登入員工編號
string TB3 = TextBox3.Text; //舊密碼
SqlConnection myConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["SqlConnStr"].ConnectionString.ToString());
myConn.Open(); //打開連接
String strSQL =
@"
select Emp_Num, Emp_PW from emp where Emp_Num = @Emp_Num
";
SqlCommand myCommand = new SqlCommand(strSQL, myConn);
myCommand.Parameters.AddWithValue("@Emp_Num", TB1.ToString());
SqlDataReader myDataReader = null;
myDataReader = myCommand.ExecuteReader();
try
{
while (myDataReader.Read())
{
if (myDataReader["Emp_Num"].ToString() == TB1.ToString() && myDataReader["Emp_PW"].ToString() == TB3.ToString())
{
Response.Write("<script>alert(\"請於1分鐘內修改密碼!\")</script>");
DetailsView1.Visible = true;
s_emp.Visible = false;
DetailsView1.ChangeMode(DetailsViewMode.Edit);
}
else
{
Response.Write("<script>alert(\"密碼錯誤,請注意大小寫!\")</script>");
return;
}
}
}
catch (Exception ex)
{
Response.Write("錯誤訊息=" + ex.ToString());
Response.Write("<script>alert(\"系統出錯,請聯絡系統管理員!\")</script>");
}
finally
{
if (myDataReader != null)
{
myCommand.Cancel();
myDataReader.Close();
}
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
myConn.Dispose();
}
}
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
TextBox TB1 = (TextBox)DetailsView1.FindControl("TextBox1"); //更新密碼確認TB1
TextBox TB4 = (TextBox)DetailsView1.FindControl("TextBox4"); //更新密碼確認TB4
Button BTN1 = (Button)DetailsView1.FindControl("Button1"); //更新按鈕
if (TB1.Text != TB4.Text)
{
Response.Write("<script>alert(\"密碼更新失敗 \\r 請確認兩次新密碼是否相同!\");history.go(-1);</script>");
e.Cancel = true;
}
else
{
Response.Write("<script>alert(\"密碼變更為:[" + TB1.Text.ToString() + "] \\r 請使用新密碼登入 \");location.href='../Login.aspx';</script>");
}
TB1.Attributes.Add("onkeydown", "if (event.keyCode==13) { document.getElementById('" + BTN1.ClientID + "').click();return true; }");
TB4.Attributes.Add("onkeydown", "if (event.keyCode==13) { document.getElementById('" + BTN1.ClientID + "').click();return true; }");
}
我有兩個Panel
主要是要做defaultbutton
Panel1的沒問題
但Panel2該如何取得DetailsView樣板模式內的btn呢?
如下Panel2的defaultbutton="????????"
謝謝
Panel1
<asp:Panel ID="Panel1" runat="server" defaultbutton="Button1">
<table>
<tr>
<td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td><asp:Button ID="Button1" runat="server" Text="變更" /></td>
</tr>
<tr>
<td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>
</table>
</asp:Panel>
Panel2
<asp:Panel ID="Panel2" runat="server" defaultbutton="????????">
<asp:DetailsView ID="DetailsView1" runat="server" "
...
...
...
</asp:DetailsView>
</asp:Panel>
如果我沒猜錯DetailsView1_ItemUpdating應該是資料更新後的事件
可是你這邊寫的應該是enter時想要觸發按鍵回傳
你可以試試在page_load時就先註冊Textbox的事件
放Page_Load會出錯
另外DetailsView1_ItemUpdating應該是更新時
DetailsView1_ItemUpdated這才是更新後
protected void Page_Load(object sender, EventArgs e)
{
DefenseLogin(); //檢查是否登入
string struNum = (string)(Session["u_Num"]); //登入工號
string uName = (string)(Session["u_Name"]); // uc 顯示使用者名稱
var value = ((Label)User_Name.FindControl("LoginInfo"));
value.Text = uName; //Response.Write(value.Text);
TextBox1.Text = struNum;
TextBox2.Text = uName;
DetailsView1.Visible = false;
TextBox TB1 = (TextBox)DetailsView1.FindControl("TextBox1"); //更新密碼確認TB1
TextBox TB4 = (TextBox)DetailsView1.FindControl("TextBox4"); //更新密碼確認TB4
Button BTN1 = (Button)DetailsView1.FindControl("Button1"); //更新按鈕
TB1.Attributes.Add("onkeydown", "if (event.keyCode==13) { document.getElementById('" + BTN1.ClientID + "').click();return true; }");
TB4.Attributes.Add("onkeydown", "if (event.keyCode==13) { document.getElementById('" + BTN1.ClientID + "').click();return true; }");
}
阿偉抱歉我講錯了
應該要在DetailsView1_ItemCommand事件裡
確認command為edit的狀態時
在註冊進元件屬性裡
為什麼不用Code behind, 在C#或vb中處理?
因為查defaultbutton都是介紹aspx做的
請問一下 C# 可以設defaultbutton嗎?
DetailsView是用在更新密碼用
希望USER 在DetailsView的PW欄位樣板模式中
編輯選項內的TextBox1,TextBox2
確認完兩次密碼後
可以直接按ENTER送出
不要再用滑鼠點button
用下方方法沒效果,也沒報錯
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
TextBox TB1 = (TextBox)DetailsView1.FindControl("TextBox1"); //更新密碼確認TB1
TextBox TB4 = (TextBox)DetailsView1.FindControl("TextBox4"); //更新密碼確認TB4
Button BTN1 = (Button)DetailsView1.FindControl("Button1"); //更新按鈕
TB1.Attributes.Add("onkeydown", "if (event.keyCode==13) { document.getElementById('" + BTN1.ClientID + "').focus();return true; }");
TB4.Attributes.Add("onkeydown", "if (event.keyCode==13) { document.getElementById('" + BTN1.ClientID + "').focus();return true; }");
}
阿偉focus()改成click()試試看