我試圖確保用戶必須從下拉框中選擇一些內容,以便我使用JavaScript來檢查他們是否從下拉框中選擇了某些內容數據被寫入數據庫。我在DetailView中有一個按鈕,我無法在這裏訪問按鈕ID。如果用戶沒有從下拉列表中選擇任何內容,我希望用戶在點擊插入按鈕時發出警告。在下拉菜單的選擇不應該是這個「 - 選擇公制名稱 - 」強制用戶從下拉框中進行選擇
<asp:DetailsView ID="DV_InputForm" runat="server" Height="69px" Width="763px" AutoGenerateRows="False"
CellPadding="3" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" OnItemInserting="DV_InputForm_ItemInserting"
OnModeChanging="DV_InputForm_ModeChanging" BackColor="#DEBA84" DefaultMode="Insert"
CellSpacing="2" Style="margin-top: 0px">
<Fields>
<asp:TemplateField HeaderText="Metric Name:">
<ItemTemplate>
<asp:Label ID="lblMetricName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MetricName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_NewMetricName" DataTextField="NAME" DataValueField="NAME"
runat="server" AutoPostBack="false" Width="400" Height="25" AppendDataBoundItems="true"
DataSourceID="DD_METRIC_DS">
<asp:ListItem Text="--Select Metric Name--" Value="--Select Metric Name"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnNew" runat="server" CausesValidation="False" CommandName="New"
Text="Create New Server Monitoring" Font-Bold="True" BorderColor="#003366" BorderStyle="Groove"
Height="30px" />
</ItemTemplate>
<InsertItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert" OnClientClick="return validate();"
Text="Insert" />
<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#FFFFCC" Font-Bold="True" ForeColor="#003366" />
這裏是我的javascript:
<script type="text/javascript">
function validate() {
var flag = true;
var dropdowns = new Array(); //Create array to hold all the dropdown lists.
var gridview = document.getElementById('<%=DV_InputForm.ClientID%>'); //GridView1 is the id of ur gridview.
dropdowns = gridview.getElementsByTagName('Select'); //Get all dropdown lists contained in GridView1.
for (var i = 0; i < dropdowns.length; i++) {
if (dropdowns.item(i).value == 'Select') //If dropdown has no selected value
{
flag = false;
break; //break the loop as there is no need to check further.
}
}
if (!flag) {
alert('Please make selection from the drop-down and click the insert button again. Thanks');
document.getElementById('<%=Button1.ClientID%>').style.visibility = 'hidden';
}
return flag;
}
</script>
<RowStyle BackColor="#CCCCCC" ForeColor="#8C4510" />
</asp:DetailsView>
我不能夠因爲它現在編譯不是這樣的:<%=Button1.ClientID%>
,我得到Button1下的紅線。這似乎是我訪問DetailView中Button1的方式是錯誤的。 <%=Button1.ClientID%>
你可以更新你的問題,並解釋當他們點擊「插入」按鈕時發生了什麼? – jeuton 2014-10-31 16:25:16